Scott Robinson
Scott Robinson

Reputation: 605

Apache mod_rewrite 'loop'/Last not functioning

I have the below Rewrite Rules however all but my last re-write have stopped working.

The folder structure looks similar to:

/
/site
/console
/requests

If I go to http://xx.com/contact, it is essentially showing me, http://xx.com/site/contact. That's working nicely. However, if I go to http://xx.com/console, this will not work. I believe it's something to do with my final rule taking over, but I cannot see how. Essentially, when I go to http://xx.com/console, it should take me to http://xx.com/console, however, I believe it's trying to take me to http://xx.com/site/console which does not exist!

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?xx.org.uk$

RewriteRule ^wp-content/uploads/2012/05/banner.png$ http://xx.org.uk/site/wp-content/uploads/2012/05/banner.png [L,R=301]
RewriteRule ^questions(.*)$ http://xx.org.uk/questions/ [L,R=301]
RewriteRule ^survey(.*)$ http://xx.org.uk/xx-2014-survey/ [L,R=301]

RewriteRule ^console/([^/.]+)$ http://xx.org.uk/console/?id=$1 [L,R=301]
RewriteRule ^requests/([^/.]+)$ http://xx.org.uk/requests/?id=$1 [L,R=301]

RewriteCond %{REQUEST_URI} !^/site/
RewriteRule ^(.*)$ /site/$1 [L,NC]

Upvotes: 1

Views: 36

Answers (2)

IMSoP
IMSoP

Reputation: 97728

Because all except your last rule have the [R] flag (for testing?) they will cause the browser to load a new URL, and a competely new request will be made to Apache.

Since this request doesn't match any of the other rules, and doesn't contain /site/, it will match the last rule.

The [L] flag, or any other flag, cannot prevent this, because Apache has no way of knowing that this new request was the result of a previous rule, and not someone typing the new URL directly into their browser.

Upvotes: 1

anubhava
anubhava

Reputation: 785256

L doesn't end rule execution the way END does but END only works in Apache 2.4+. L only ends current rule otherwise.

For previous Apache version tweak your last rule as:

RewriteCond %{REQUEST_URI} !^/(site|questions|console|requests|xx-2014-survey)/ [NC]
RewriteRule ^(.*)$ /site/$1 [L,NC]

Upvotes: 1

Related Questions