Giorgio
Giorgio

Reputation: 1970

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

When I comment the second RewriteRule statement, everything works fine.
Is there a precedence in url rewrite rules? What am I missing?

Upvotes: 1

Views: 350

Answers (1)

anubhava
anubhava

Reputation: 785256

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]

Upvotes: 1

Related Questions