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=

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

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题