user2983512
user2983512

Reputation: 73

HtAccess: Force WWW but keep subdomain mask

I'm trying to force .www so Google doesn't pick up my website twice but when I do my subdomains no longer mask using [P].

RewriteEngine on

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} !^((www\.)?)domain\.com [NC] 
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/website/%1$1 [L,NC,P]

When I visit a masked domain it simply just redirects and no longer masks. If I take out the top part the subdomains work fine but it then doesn't force .www

Upvotes: 1

Views: 165

Answers (1)

anubhava
anubhava

Reputation: 785471

Try changing the order of rules:

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/website/%1/$1 [L,P]

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

Upvotes: 1

Related Questions