【问题标题】:RewriteRule precedence in htaccesshtaccess 中的 RewriteRule 优先级
【发布时间】:2015-01-27 04:31:37
【问题描述】:

我正在尝试设置 .htaccess 文件来重写 URL。

设置

这是我当前的 .htaccess 设置:

RewriteEngine On
Options -Multiviews

RewriteRule ^admins/login.html$ mvc.php?rt=admins/login
RewriteRule ^([^/]+)/([^/]+).html$ mvc.php?rt=$1&id=$2

这个想法是将admins/login.html 模式与第一个 RewriteRule 语句匹配,将其他xxxx/xxxx.html 模式与第二个匹配。


检查结果

要检查结果,我只需在 mvc.php 上打印接收到的值

echo 'rt = '.$_REQUEST['rt'].', id = '.$_REQUEST['id']; 

输入admins/login.html 模式

  • 我的目标:rt = admins/login, id =
  • 我得到了什么:rt = mvc.php, id = login

当我评论第二个 RewriteRule 语句时,一切正常。
url重写规则有优先级吗?我错过了什么?

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

    这是因为您的规则不止一次执行。要停止这种行为,请像这样使用 .htaccess:

    Options -Multiviews
    RewriteEngine On
    
    # REDIRECT_STATUS is set to 200 after first rule execution
    RewriteCond %{ENV:REDIRECT_STATUS} .+
    RewriteRule ^ - [L]
    
    RewriteRule ^admins/login\.html$ mvc.php?rt=admins/login [L,QSA,NC]
    
    RewriteRule ^([^/]+)/([^/]+)\.html$ mvc.php?rt=$1&id=$2 [L,QSA,NC]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 2012-03-03
      相关资源
      最近更新 更多