Forum Moderators: phranque

Message Too Old, No Replies

Wildcard Subdomain Redirect weirdness

www.example.com -> www.www.example.com bad

         

whoisgregg

4:47 pm on Sep 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a domain that will be accessed via a number of subdomains. In all cases, I would like to add a www. to the beginning of the domain if it doesn't yet exist. So, I added this is the code to the site's root .htaccess file:

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

It works as expected for this input:

test.example.com -> www.test.example.com good
blah.example.com -> www.blah.example.com good

However, it also does this, which I don't want:

www.example.com -> www.www.example.com bad

I have also tried a less specific rule for the first RewriteCond:

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

I've got other code in the .htaccess but it exhibits the behavior even if I strip all that out and just use the code shown above. Thoughts? :/

jdMorgan

4:57 pm on Sep 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your first RewriteCond is failing because you closed the variable name with a right parenthese ")" instead of a right bracket "}" ...

Jim

whoisgregg

5:34 pm on Sep 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



::smacks forehead::

Yeah, I sure did. Been staring at it long enough for that little detail to just blur away.

Thank you for your help, Jim. :)

jdMorgan

6:55 pm on Sep 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest that you also take care of trailing-dot hostnames (FQDNs) and appended port numbers:

RewriteEngine on
#
# Externally redirect to add "www" to main domain or subdomains
RewriteCond %{HTTP_HOST) !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([a-z]+\.)?example\.com
RewriteRule (.*) http://www.%1example.com/$1 [R=301,L]
#
# Externally redirect to remove trailing dot and/or port number
RewriteCond %{HTTP_HOST} ^(([a-z]+\.)+)example\.com(\.¦:[0-9]*)
RewriteRule (.*) http://%1example.com/$1 [R=301,L]

Note that I removed "RewriteBase /". This is the default setting, and you should not have to specify it.

Also note that I changed both rules.

Replace the broken pipe "¦" character in the RewriteCond pattern of the second rule with a solid pipe before use; Posting on this forum modifies the pipe character.

Jim

whoisgregg

4:23 pm on Sep 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good points on both. Will definitely use that code. :)

Thanks again Jim.

nigelheap

11:37 pm on Dec 1, 2008 (gmt 0)

10+ Year Member



 RewriteCond %{HTTP_HOST) !^www\.example\.com 

 RewriteCond %{HTTP_HOST} ^www\.([a-z]+\.)?example\.com 

 RewriteRule (.*) http://%1example.com/$1 [R=301,L] 

This worked quite well for me...