繁体   English   中英

.htaccess根域不需要www,而子域不需要www

[英].htaccess root domain require www and sub domain not require www

我想设置如下域:

example.com-> www.example.com

example.com/page-> www.example.com/page

www.example.com->不重定向

www.example.com/page->不重定向


subdomain.example.com->不重定向

subdomain.example.com/page->不重定向

www.subdomain.example.com-> subdomain.example.com

www.subdomain.example.com/page-> subdomain.example.com/page


http和https也应该起作用。

我从.htaccess获取了代码,要求域为WWW,但如果没有硬编码,则允许子域(如果存在)

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

它对所有人都可以很好地工作,但以下情况除外:

www.subdomain.example.com-> www.subdomain.example.com

www.subdomain.example.com/page-> www.subdomain.example.com/page

子域应该的“www。” 前缀,但仍带有“ www”。 字首。

感谢您的教程。

在您拥有的规则之后添加此

RewriteCond %{HTTPS} ^on$
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+\.[^.]+)$
RewriteRule ^ https://%1%{REQUEST_URI} [L]

RewriteCond %{HTTPS} !^on$
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+\.[^.]+)$
RewriteRule ^ http://%1%{REQUEST_URI} [L]

我还没有测试过,但是我很确定我的理论是可靠的。 至少假设您示例中的HTTPS部分有效。

暂无
暂无

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

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