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 ^(.*)$ http://domain2.net/$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 http://domain2.net/123 and then (when the host name doesn't fit the second rule anymore) to http://domain2.net/beta/forward.php?id=123. 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]+.*)$ http://domain2.net/$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.

2 Answers 2


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 domain1.net…

RewriteRule ^(.*)$ http://domain2.net/$1 [R=301,L] # redirect to domain2.net. End [L]

  • 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, 2009 at 19:36

I think you should check the flags of the rewrite rules (http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule) pay attention to what C and L flags do.

  • Thanks! I've been looking for a documentation of all the flags.
    – LKM
    Aug 27, 2009 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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