user1683128
user1683128

Reputation: 1

.htaccess unmasked/forced wildcard subdomain redirect

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.

Upvotes: 0

Views: 648

Answers (2)

Oussama Jilal
Oussama Jilal

Reputation: 7739

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.

Upvotes: 0

anubhava
anubhava

Reputation: 785146

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]

Upvotes: 1

Related Questions