0

I want to edit the .htaccess file to create an unmasked wildcard redirect of all subdomains to the main www subdomain.

i.e. *.domain.com => www.domain.com

e.g. www.example.domain.com => www.domain.com

When the person types in www.example.domain.com in the browser address bar, I want it to redirect unmasked so that the URL actually visibly changes to www.domain.com.

My present .htaccess file (shown below) forces the 'www' for all 'without www' traffic, i.e. http://domain.com becomes http://www.domain.com, but can't figure out what RewriteCond etc. commands to use for the above requirement.

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

Any help would be greatly appreciated! Thanks in advance.

Asciiom
  • 9,867
  • 7
  • 38
  • 57

2 Answers2

1

Have your .htaccess like this:

RewriteEngine on
RewriteBase /

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

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks for your input @anubhava. I tried what you suggested, and the first part seems to work but I didn't get the 2nd part to work (i.e. the *.domain.com => www.domain.com). I did empty my browser cache first. Any ideas why this might be please? Thanks again. – user1683128 Sep 19 '12 at 13:35
  • Can you post your latest .htaccess and relevant part of access.log file with your question? – anubhava Sep 19 '12 at 15:00
0

It is simple, try :

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

You won't need your rule that adds the www also, so just remove it.

Oussama Jilal
  • 7,669
  • 2
  • 30
  • 53
  • Thanks for the input @Yazmat. Not sure what's going on here but I'm getting the same problem as with anubhava's code. domain.com => www.domain.com ok, but example.domain.com still results in an error. Keep emptying the browser cache each time. Cheers. – user1683128 Sep 19 '12 at 13:41
  • I am not sure if it makes any difference or not, but I'm looking to redirect all subdomains to the www subdomain, whether they actually exist as folders in the main 'public_html' folder or not. I deleted the redirect associated with the root domain with hostgator also, as this can be done via the .htaccess file but so far no change. – user1683128 Sep 19 '12 at 13:59
  • It doesn't point anywhere, the browser tries to load it and fails and the ISP redirects the browser to a BT Yahoo (my ISP) error page with a message stating 'sorry the website example.domain.com cannot be found.' – user1683128 Sep 19 '12 at 14:08
  • Then your subdomain is not DNS resolved, either you did not create it at all or you did not configure it as it should – Oussama Jilal Sep 19 '12 at 14:17
  • I have some subdomains/add on domains folders with the main public_html folder where the main domain.com site resides. These are separate domain web sites. I have not modified these .htaccess files, only the .htaccess file in the main public_html folder. Hostgator allows you to reach these other domains (e.g. otherdomain.com) by entering the URL www.otherdomain.domain.com. I am trying to redirect all such entries to the main domain.com page so that you can only enter the otherdomain.com site by this direct URL and not as a subdomain of domain.com, if that makes sense. – user1683128 Sep 19 '12 at 14:25
  • so, I'd like to redirect any and all subdomain URLs to the main domain.com URL, whether they exist or not (e.g. example.domain.com or xylgkersdfjlkjtl.domain.com), as stated above. I'm not sure if this is possible or not. – user1683128 Sep 19 '12 at 14:26
  • If you want that , you need first to make any subdomain resolve to your parent domain ip. – Oussama Jilal Sep 19 '12 at 14:40
  • do you mean to edit the existing .htaccess files for the existing subdomains with the above code? I tried that and it works ok (could also do this from the hostgator cpanel). So how do we do an unmasked redirect on subdomain names that don't exist? In order to disguise the above subdomain redirects? Thanks for your input. – user1683128 Sep 19 '12 at 15:04
  • I did not understand what you are trying to do in your last question – Oussama Jilal Sep 19 '12 at 15:07