user2313408
user2313408

Reputation:

Force www. without forcing it on subdomains?

I am currently using ht.access to force WWW Prefix on my domain name....

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

I would like I keep that, but for my subdomains, it displays as

www.subdomain.mydomain.com but I want it without the www. on my subdomain....

Currently: http://www.subdomain.mydomain.com Wanting: http://subdomain.mydomain.com

Is there any known good edits to that code or any other .htaccess code to do what I need?

Upvotes: 1

Views: 158

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

You'll have to either be explicit about the domain name or assume only a specific number of TLD's (or it gets harder otherwise).

Explicit about the domain:

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

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

Or only 1 TLD:

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

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

Upvotes: 1

Related Questions