【问题标题】:Loosening .htaccess file's RewriteRule(s) to also access URL without a trailing slash松开 .htaccess 文件的 RewriteRule(s) 以访问不带斜杠的 URL
【发布时间】:2013-03-10 14:14:55
【问题描述】:

我的.htaccess 文件总是需要带有 斜杠 的 URL,例如:

http://localhost/menjaraz/webroot/about/
http://localhost/menjaraz/webroot/2013/03/21/you-are-my-heart-you-are-my-soul/

工作,否则会触发 404 错误。

我希望放宽规则,以便没有结束斜杠的网址也可以这样做。

摘录

RewriteBase /menjaraz/webroot
RewriteRule ^([^/]*)/$ index.php?page=$1
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ index.php?a=$1&b=$2&c=$3&page=$4

我应该怎么做?我对正则表达式和 mod_rewrite 都不太满意。

提前感谢。

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    您需要使用? 将最后一个斜杠设为可选:

    RewriteBase /menjaraz/webroot
    RewriteCond %{REQUEST_URI} !index.php
    RewriteRule ^([^/]*)/?$ index.php?page=$1
    RewriteCond %{REQUEST_URI} !index.php
    RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ index.php?a=$1&b=$2&c=$3&page=$4
    

    ? 使前面的字符或组是可选的。因为没有尾部斜杠,所以您需要添加一个条件以确保 index.php 不会被重写。

    【讨论】:

      猜你喜欢
      • 2014-09-28
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 2012-08-08
      • 2015-09-20
      • 2014-12-22
      • 2013-07-16
      • 1970-01-01
      相关资源
      最近更新 更多