【问题标题】:.htaccess to redirect index.* and rewrite the url into a query string.htaccess 重定向 index.* 并将 url 重写为查询字符串
【发布时间】:2012-09-19 14:14:11
【问题描述】:

我目前正在尝试在我网站的 /sub/ 文件夹中的 .htaccess 文件中使用两个重写规则。一种是重定向以删除 url 末尾的 index.php、index.html 等,例如/sub/index.php -> /sub/:

RewriteCond %{REQUEST_URI} ^(.*)/index\.[^\.]+$
RewriteRule index.* %1/ [r=301,L]

另一种是重写 php 的 url 以获取查询字符串,例如/sub/testing-testing -> /sub/index.php?field=testing-testing:

RewriteRule ^(.*)$ index.php?command_name=$1 [L,QSA]

这些似乎单独工作没有问题,但我就是不知道如何让它们一起工作。

我可以看到第二条规则可能出了什么问题,因为第一个规则匹配“index(...)”,所以我尝试了这些不同的变体,但我总是以无休止的重定向循环或被重定向回网站的根目录。

【问题讨论】:

    标签: .htaccess mod-rewrite redirect


    【解决方案1】:

    删除index.php 时,您需要匹配实际请求,而不是URI。两条规则不能一起工作的原因是因为 mod_rewrite 一直循环遍历所有规则,直到 URI 停止更改。第 2 条规则将 URI 重写为 index.php,第 1 条规则匹配并删除它,然后第 2 条规则将其重写回来,等等。

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.(php|html?)
    RewriteRule ^ /%1 [R=301,L]
    

    那么您应该可以保留第二条规则。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 2012-12-21
      • 2012-11-15
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多