Forum Moderators: phranque

Message Too Old, No Replies

Landing domain redirected to main domain directory

         

a1tsal

6:32 pm on Jul 9, 2007 (gmt 0)

10+ Year Member



Hi,

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]

jdMorgan

8:30 pm on Jul 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code should work. Look for other rules (such as the one that internally rewrites foo.com to example.com/foo) and modify those rules to comprehend the requested page path.

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]

Here the (www\.) is made optional by the following "?" so the pattern matches either the www or non-www domain and the literal periods are escaped as required to match only a period.

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]

a1tsal

7:02 pm on Jul 11, 2007 (gmt 0)

10+ Year Member



Thank you very much!

Moving the rule to the top of the .htaccess file solved the problem.

I've adopted your more efficient regexp alternative to [OR].

Thanks again,

David