简体   繁体   English

htaccess中的RewriteRule优先级

[英]RewriteRule precedence in htaccess

I'm trying to setup .htaccess file to rewrite an URL. 我正在尝试设置.htaccess文件以重写URL。

Setup 设定

Here's my current .htaccess setup: 这是我当前的.htaccess设置:

RewriteEngine On
Options -Multiviews

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

The idea is to match the admins/login.html pattern with the first RewriteRule statement and other xxxx/xxxx.html patterns with the second. 这个想法是使admins/login.html模式与第一个RewriteRule语句匹配,而其他xxxx/xxxx.html模式与第二个xxxx/xxxx.html


Check result 检查结果

To check the result, I simply print the received value on mvc.php 要检查结果,我只是在mvc.php上打印接收到的值

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

Typing the admins/login.html pattern 键入admins/login.html模式

  • my goal: rt = admins/login, id = 我的目标: rt = admins/login, id =
  • what I get: rt = mvc.php, id = login 我得到什么: rt = mvc.php, id = login

When I comment the second RewriteRule statement, everything works fine. 当我评论第二条RewriteRule语句时,一切正常。
Is there a precedence in url rewrite rules? 网址重写规则中是否有优先级? What am I missing? 我想念什么?

It is because your rules are executing more than once. 这是因为您的规则执行了不止一次。 To stop this behavior have your .htaccess like this: 要停止此行为,请使您的.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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM