Force www to non-www on a subdomain in WordPress?

Forcing www to non-www is simple at the primary domain level.

What about at the subdomain level?

If I am correct the “issue” is that it is at two levels of depth so you can’t force a subdomain to be non-www

This is what I mean:

https://my.domain.com < correct
https://www.my.domain.com < incorrect and trying to force to non-www

3 Answers
3

Forcing www to non-www is simple at the primary domain level.

It’s actually the same for the subdomain (depending on how you’ve written the directives in the first place). If the hostname (main domain or subdmomain) starts with www. then remove it.

(Forcing non-www to www for subdomains and main domains is a little more tricky if you want a generic single directive solution.)

The following removes the www subdomain from any hostname.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

In addition… If your subdomains are pointing to subdirectories off the main domain (eg. example.com/subdomain) then these subdirectories are probably also accessible and would need to be redirected. (Although search engines shouldn’t discover these subdirectories, or the www subdomain for that matter, unless it was leaked in some way.)

Leave a Comment