drjmrg8766 2019-07-03 07:05
浏览 207
已采纳

URL-Rewrite如何与3 Params一起使用?

i have build a URL-Routing FrontController in PHP. All works fine, but now i find a error, if i have more params then 2 it dont works, for example:

This URL works: "www.comelio.com/business-intelligence/anleser/"

but this URL dont works: "www.comelio.com/business-intelligence/data-mining/anleser/"

My Rewrite Rule:

RewriteRule ^([\w-]+)/?([\w-]+)/?([\w-]+)/?([\w-]+)? index.php?lang=$1&rubrik=$2&unterrubrik=$3&seite=$4

Here my Routing if-else code:

if($seite == null) {
  $filename = "{$rubrik}.html";
  $xdmvalue = $saxonProc->createAtomicValue($filename);
  $xsltProc->setParameter("articlePfad", $xdmvalue);

  if(in_array($filename, $filelist)) {
    $xmlFile = $dir . "/" . $filename;
    $xsltProc->setSourceFromFile($xmlFile);
  } else {
    echo "404";
  }
} else if(isset($seite) && isset($rubrik)){
  $filename = "{$rubrik}_{$seite}.html";
  $xdmvalue = $saxonProc->createAtomicValue($filename);
  $xsltProc->setParameter("articlePfad", $xdmvalue);

  if(in_array($filename, $filelist)) {
    $xmlFile = $dir . "/" . $filename;
    $xsltProc->setSourceFromFile($xmlFile);
  } else {
    echo "404";
  }
} else if(isset($seite) && isset($rubrik) && ($unterrubrik)){
  $filename = "{$rubrik}_{$unterrubrik}_{$seite}.html";
  $xdmvalue = $saxonProc->createAtomicValue($filename);
  $xsltProc->setParameter("articlePfad", $xdmvalue);

  if(in_array($filename, $filelist)) {
    $xmlFile = $dir . "/" . $filename;
    $xsltProc->setSourceFromFile($xmlFile);
  } else {
    echo "404";
  }
}

Before i write this code so the second parameter works only, now only the third parameter works, for example now works: "comelio.com/business-intelligence/data-mining/anleser"

And this dont works: "comelio.com/business-intelligence/anleser"

  • 写回答

1条回答 默认 最新

  • dook0034 2019-07-03 07:51
    关注

    Have a look at the htaccess Tester here (Make sure to add http in the URL field).

    In your Rewrite Condition, you only make the slashed optional. Thus, the rewriter will always split up the request url to match 4 parts. Try changing your rule to

    RewriteRule ^([\w-]+)/?([\w-]+)?/?([\w-]+)?/?([\w-]+)? index.php?lang=$1&rubrik=$2&unterrubrik=$3&seite=$4
    

    (Note the question marks behind the ([\w-]+))

    This will give you
    http://www.comelio.com/index.php?lang=business-intelligence%26rubrik=data-mining%26unterrubrik=anleser%26seite=

    and

    http://www.comelio.com/index.php?lang=business-intelligence%26rubrik=anleser%26unterrubrik=%26seite=

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改