Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

http://domain1.net/123

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.

share|improve this question
add comment

2 Answers 2

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

RewriteRule ^(.*)$ http://domain2.net/$1 [R=301,L] # redirect to domain2.net. 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
add comment

I think you should check the flags of the rewrite rules (http://httpd.apache.org/docs/2.0/mod/mod%5Frewrite.html#rewriterule) 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
add comment

Your Answer

 
discard

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.