Precedence of RewriteRules in .htaccess file on Apache Server

Solution 1:

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]

Solution 2:

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.