Welcome to WebmasterWorld Guest from 54.196.100.224

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

No not rewrite subdomains

     
4:26 pm on May 7, 2012 (gmt 0)

New User

joined:Apr 12, 2012
posts:19
votes: 0


I have the following in my .htaccess file

# Force www
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


It was put there for SEO reasons to ensure that traffic to http://example.com was redirected to the www. address consistently.

I am now developing a subdomain and of course when I type in subdomain.example.com it helpfully directs me to www.example.com.

Can somebody help me get the syntax right so that only requests to http://example.com are redirected to www.example.com and not any other subdomains.
6:20 pm on May 7, 2012 (gmt 0)

Administrator from US 

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 25, 2005
posts:14650
votes: 94


I would think changing from
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
to
RewriteCond %{HTTP_HOST} ^(example\.com)?$
Would do the trick
7:59 pm on May 7, 2012 (gmt 0)

New User

joined:Apr 12, 2012
posts:19
votes: 0


Thank you, that did the trick.
2:16 am on May 8, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13204
votes: 346


Most specific to most general. Start with anything that applies to the subdomain, get it out of the way and then carry on as usual with the primary domain.

What if your requested host is example.com:80 or the like?
6:42 am on May 8, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


There was a reason that the pattern was !^(www\.example\.com)?$ which is now negated by using something else.

Use this:

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