Forum Moderators: phranque
I manage a site with several "sub-sites" that are directories under the main site. So the main site is example.com and there are subsites example.com/foo/ and example.com/bar/ .
The subsites have their own domain names (used in print advertising), so that foo.com redirects to example.com/foo/ and bar.com redirects to example.com/bar/ .
This works, but I would like to have foo.com/something.html redirect to example.com/foo/something.html, and I can't figure out how to make that happen (after two or three hours of trying).
My code is:
RewriteCond %{HTTP_HOST} ^foo.com [OR]
RewriteCond %{HTTP_HOST} ^www.foo.com
RewriteRule ^(.*) http://example.com/foo/$1 [R=301,L]
This redirects correctly requests for just foo.com (or www.foo.com), but foo.com/something.html apparently gets rewritten as http://example.com/foo/index.php for some reason (?!?) and fails.
Ought my code to work? Is there interference from some system config file? Or is there a bug in my code?
Many thanks... and sorry if this is a newbie question; I have looked pretty hard for an answer and not found it.
David
[edited by: jdMorgan at 3:54 pm (utc) on July 12, 2007]
[edit reason] example.com [/edit]
In all of your rewrites and redirects taken as a whole, the most-specific external redirects should be first, then the least specific redirects, then the most-specific internal rewrite, followed by the least-specific. This prevents an earlier internal rewrite ("/" to "/index.php" for example) from being "exposed" to the client by a later external redirect.
The only changes I'd make to your code snippet would be for efficiency:
RewriteCond %{HTTP_HOST} ^(www\.)?foo\.com
RewriteRule (.*) http://example.com/foo/$1 [R=301,L]
By the way, it would be a very good idea to force all requests to your canonical domain -- either "www" or non-www, whichever you choose. This eliminates the need to handle both variants in all hostname-dependent rules, and also prevents ranking problems due to the same content appearing in multiple domains.
Jim
[edited by: jdMorgan at 3:55 pm (utc) on July 12, 2007]