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 数电几道习题,写出作答过程
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52
  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库
  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频
  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 前端高拍仪调用问题报错