I’m trying to rewrite any URL that has a non empty REQUEST_URI to another domain while preserving the REQUEST_URI. The idea being that only http://www.example.com exists on this domain and any other request should go to http://www.example2.com.
I’ve verified that http://www.example.com has ‘/’ as its REQUEST_URI.
e.g.
www.example.com -> should not be rewritten
www.example.com/test.php -> should be rewritten to www.example2.com/test.php
www.example.com/test2.php?id=123 -> should be rewritten to www.example2.com/test2.php?id=123
[updated]
I’ve tried to compare the REQUEST_URI to ‘/’ but that redirects everything:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.+)$ http://www.example2.com/$1 [L,QSA]
I’ve tried to check if the file or directory exists then redirect it but that is redirecting even the base page (www.example.com)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.example2.com/$1 [L,QSA]
I’ve tried to check if there’s content after the slash but that redirects nothing:
RewriteEngine on
RewriteRule ^.+/(.+)$ http://www.example2.com/$1 [L,QSA]
Can anyone come up with a working solution to this? It seems like it should be very simple yet I can’t seem to get it working.
Thanks.
Solution:
FYI: The solution I ended up going with was:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.example2.com/$1 [R,L,QSA]
The reason being is that if I have any images on http://www.example.com, they would be rewritten to http://www.example2.com. The [R] is what made the base URL redirect issue work properly. Credit to poncha for pointing that out.
Without the
!, yourRewriteCondis a positive match, not negative.Also, added
Rflag to force apache send a redirect to browser