Forum Moderators: phranque

Message Too Old, No Replies

rewriting subdomains

getting a subdomain back to the main domain

         

Etruscan

1:56 am on Sep 9, 2007 (gmt 0)

10+ Year Member



Here's my dilemma. I used to use a variety of subdomains, and now I'm not. However, I'm still getting requests for those domains of course, so I'd like to rewrite them to the appropriate page. I thought this would work:

RewriteRule ^category.example.com/article/(.*)$ www.example.com/category/$1/ [R=301]

...but it doesn't.

[edited by: jdMorgan at 2:43 am (utc) on Sep. 9, 2007]
[edit reason] example.com [/edit]

jdMorgan

2:47 am on Sep 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteRule "sees" only the local URL-path, that is, the URL with the path to the current directory (where the file containing the RewriteRule resides) removed.

So in short, RewriteRule cannot see the domain name.

You must use a RewriteCond to examine the requested hostname (taken from the "Host:" header received from the client). Assuming that the code resides in example.com/.htaccess:
[code]
RewriteCond %{HTTP_HOST} ^category\.example\.com
RewriteRule ^article/(.+)/?$ http://www.example.com/category/$1/ [R=301,L]
[code]

Because you said you used to use multiple subdomains, you may want to redirect more of them using the same rule:
[code]
RewriteCond %{HTTP_HOST} ^(category)\.example\.com [OR]
RewriteCond %{HTTP_HOST} ^(category2)\.example\.com [OR]
RewriteCond %{HTTP_HOST} ^(classification)\.example\.com
RewriteRule ^article/(.+)/?$ http://www.example.com/%1/$1/ [R=301,L]
[code]
Note that the last RewriteCond must not have an [OR] flag on it, since the [OR] flag operates between the RewriteCond line that it is on and the following RewriteCond; Trying to [OR] a RewriteCond with a RewriteRule doesn't make sense anyway.

Jim

Etruscan

2:55 am on Sep 9, 2007 (gmt 0)

10+ Year Member



The code looks good, but you mentioned that this is assuming it resides in .htaccess - when it actually resides inside the virtual host in httpd.conf. Any differences?

jdMorgan

3:06 am on Sep 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add a leading slash to the URL-path pattern of the RewriteRule, e.g.

RewriteRule [b]^/ar[/b]ticle/.....

Jim

Etruscan

3:09 am on Sep 9, 2007 (gmt 0)

10+ Year Member



This is why you're the best. Worked like a charm JD.

Cheers!

jdMorgan

3:16 am on Sep 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you for the compliment, but I have learned from others, including many who have posted here... :)

Jim