简体   繁体   中英

redirect to www if there's no subdomain in the url

I have

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

to redirect to www.mysite.com if www is not in the url. the problem with that is that it will also add www to subdomains for example

test.mysite.com will be changed to www.test.mysite.com

how can I tweak that rule to add www only if there's no subdomain set?

how can I twaek that rule to add www only if there's no subdomain set?

Set condition to make it work for your main domain only:

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Make sure to test this in a new browser to avoid old cache.

Regex ^[^.]+\\.[^.]+$ will match any domain with a single DOT so it will skip subdomains.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM