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

subdomain.example.com to example.com

Msg#: 4673331 posted 1:27 am on May 22, 2014 (gmt 0)

I want blog.example.com and blog.example.com/any_garbage_here
to redirect to www.example.com

I have put the following in my htaccess.
index.html does go to root so that part is fine.
non-www does go to www so that part is also fine.
but blog.example.com does not go to example.com
why is it not working? how do I modify this to get it to work?

RewriteEngine On

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

# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index\.html
RewriteRule ^(.*)index\.html$ /$1 [R=301,L,NC]



WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

Msg#: 4673331 posted 3:41 am on May 22, 2014 (gmt 0)

Do you have other subdomains that you need to keep? If not, you can simplify everything at this point by letting all redirects fall under a single domain-name-canonicalization rule. It looks like this:

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

Typically this will be your very last redirect, where the second-to-last is the index.html redirect. The rules you've quoted are out of order. If there are no surviving subdomains-- as implied by the quoted rules-- the index.html redirect is also a single line plus condition. (The condition may not even be needed if the [NS] flag works for you. Try it.) This one goes right before the domain-name rule.

Incidentally, forms like
are never necessary. Just leave off the anchor along with the .* part. Personally I wouldn't use an [NC] here. If someone asks for "INDEX.html", let them get the 404 they deserve ;)

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