Reputation: 23892
Why does:
http://example.com/robots.txt
redirect to:
http://www.example.com/mvc/view/robots/live-robots.txt
with these rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^robots.txt /mvc/view/robots/live-robots.txt [L]
#.... 20 irrelevant lines for mobile rewrites
# Force the "www."
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
loading this however:
http://www.example.com/robots.txt
rewrites the live-robots.txt
as expected.
Shouldn't the L
flag stop the redirect in both cases and not get to the latter rule?
The L flag can be useful in this context to end the current round of mod_rewrite processing.
https://httpd.apache.org/docs/current/rewrite/flags.html
Current execution paths:
and then
I'm pretty sure it isn't a regex issue but here's testing of that too, https://regex101.com/r/eI9aC4/1.
Upvotes: 1
Views: 618
Reputation: 785246
Flag L
doesn't stop other rules to execute. It just acts as continue
in a while
loop and makes mod_rewrite
loop to run again.
Your rules need to be reversed in order. In general keep redirect rules before internal rewrite ones:
RewriteEngine On
RewriteBase /
# Force the "www."
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^robots\.txt$ mvc/view/robots/live-robots.txt [L,NC]
#.... 20 irrelevant lines for mobile rewrites
Make sure to clear your browser cache for this change.
However if you have Apache 2.4+ then you can use END
flag to completely stop all the rules below:
RewriteRule ^robots\.txt$ mvc/view/robots/live-robots.txt [END]
Upvotes: 1