Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a number of RewriteRules in my .htaccess file. However, one specific rule is only executed if I remove another specific rule, regardless of how I order the two rules.

Here's the rule which seems to have "lower" priority:

RewriteRule ^([0-9]+)$ /beta/forward.php?id=$1 [L]

And here's the rule which always takes precedence:

RewriteCond %{HTTP_HOST} ^domain1\.net$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

Both domain1 and domain2 actually point to the same web site. So, whenever both rules are in the .htaccess file and I access

The second rule gets executed first and I get two redirects, first to and then (when the host name doesn't fit the second rule anymore) to I've tried fixing the second rule so it is never executed for urls which only contain numbers, but I must be doing something wrong, because it still gets executed:

RewriteCond %{HTTP_HOST} ^ domain1\.net$ [NC]
RewriteRule ^(.*[^0-9]+.*)$$1 [R=301,L]

Only be removing the two lines from the .htaccess file can I get the first rule to handle the request right away.

Any help would be greatly appreciated.

share|improve this question
up vote 3 down vote accepted

Your rules indicate these procedures:

If the URL is only numbers, go to the beta URL followed by those numbers and stop.

If you visit domain1, go to the same URL at domain2 and stop.

When an L is encountered or the rules are finished, the processing ends. Anything else happening is a new connection from the browser. This is probably what is confusing for you—the order matters, then the matches. The L should take care of the problem you have when they're in order:

RewriteEngine on

RewriteRule ^([0-9]+)$ /beta/forward.php?id=$1 [L] # end of processing [L] if it's only numeric

RewriteCond %{HTTP_HOST} ^domain1\.net$ [NC] # if case-insensitive match of…

RewriteRule ^(.*)$$1 [R=301,L] # redirect to End [L]

share|improve this answer
Right, I got confused by the fact that the browser started a second connection which I didn't see in its UI anywhere. – LKM Aug 27 '09 at 19:36

I think you should check the flags of the rewrite rules ( pay attention to what C and L flags do.

share|improve this answer
Thanks! I've been looking for a documentation of all the flags. – LKM Aug 27 '09 at 19:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.