简体   繁体   中英

RewriteRule precedence in htaccess

I'm trying to setup .htaccess file to rewrite an URL.

Setup

Here's my current .htaccess setup:

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.


Check result

To check the result, I simply print the received value on mvc.php

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

Typing the admins/login.html pattern

  • my goal: rt = admins/login, id =
  • what I get: rt = mvc.php, id = login

When I comment the second RewriteRule statement, everything works fine.
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:

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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