Welcome to WebmasterWorld Guest from 54.211.238.44

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

subdomain.example.com to example.com

     
1:27 am on May 22, 2014 (gmt 0)

New User

joined:Apr 14, 2014
posts:17
votes: 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]
3:41 am on May 22, 2014 (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:13531
votes: 403


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 ;)