Forum Moderators: phranque

Message Too Old, No Replies

Redirect multiple subdomains with www

Redirect

         

mspot

10:02 am on Jun 16, 2005 (gmt 0)

10+ Year Member



I recently moved to a new server and i had to move my clients (subdomains) as well. The old server i was on supported both http://subdomain.example.com and http://www.subdomain.example.com. I know that this is not the correct way to call a page but a lot of my clients designed their pages using the latter (www.subdomain.domain.com).
Now the links are not working any more. So at the request of a particular client I tried the following in my .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.subdomain.example.com
RewriteRule (.*) http://subdomain.example.com/$1 [R=301,L]

Everything is working perfect for this subdomain but i want to write a rule that covers everybody on our site.

Can somebody please help?

Thank you

[edited by: jdMorgan at 1:57 pm (utc) on June 16, 2005]
[edit reason] Example.com [/edit]

jdMorgan

1:58 pm on Jun 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mspot,

Welcome to WebmasterWorld!

The key here is to create a back-reference to the subdomain pattern matched in your RewriteCond, and to include that back-reference in your RewriteRule substitution. This is almost exactly the same thing that you're doing with the filename back-referenced by "$1". The only difference is that back-references to RewriteConds use %1 through %9, rather that $1 through $9 as used for RewriteRule back-references:


RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule (.*) http://%1.example.com/$1 [R=301,L]

When specifying literal characters in patterns that are regular-expressions operators, be sure to escape them by preceding them with "\" as shown in the RewriteCond above.

See the documentation cited in our forum charter [webmasterworld.com] for more information.

Jim

[edited by: jdMorgan at 5:53 pm (utc) on June 16, 2005]

mspot

4:20 pm on Jun 16, 2005 (gmt 0)

10+ Year Member



It does not work. (error: Cannot find server)
Any more ideas?

jdMorgan

5:53 pm on Jun 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I got in too much of a hurry and left out the parentheses in the RewriteCond pattern.

See the corrected code above.

Jim

mspot

9:33 pm on Jun 16, 2005 (gmt 0)

10+ Year Member



Thanks a lot Jim you've been very helpful.

It works ok but on top of the .htaccess I had to add for each subdomain a CNAME entry to DNS in the form of *.subdomain.domain.com pointing to subdomain.domain.com (fedora 2.0 plesk 7.5).
If I don't do this it simply doesn't work.

Thanx again

jdMorgan

12:01 am on Jun 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure, without the DNS entries, the requests will never get to your server, so it doesn't matter what you do in the code if the DNS doesn't resolve the (sub)domain names to your IP address. It's a two-step process, and this is simply how the Web works.

Jim