homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

No not rewrite subdomains

 4:26 pm on May 7, 2012 (gmt 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)

I would think changing from
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteCond %{HTTP_HOST} ^(example\.com)?$
Would do the trick


 7:59 pm on May 7, 2012 (gmt 0)

Thank you, that did the trick.


 2:16 am on May 8, 2012 (gmt 0)

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)

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]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved