简体   繁体   English

如果网址中没有子域,则重定向到www

[英]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. 如果网址中没有www,则重定向到www.mysite.com。 the problem with that is that it will also add www to subdomains for example 问题在于它还会将www添加到子域中

test.mysite.com will be changed to www.test.mysite.com test.mysite.com将更改为www.test.mysite.com

how can I tweak that rule to add www only if there's no subdomain set? 仅在未设置子域的情况下,如何调整该规则以添加www?

how can I twaek that rule to add www only if there's no subdomain set? 仅在未设置子域的情况下,如何修改该规则以添加www?

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. 正则表达式^[^.]+\\.[^.]+$将与具有单个DOT的任何域匹配,因此它将跳过子域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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