[英]Redirecting non-www to www, but keeping other sub-domains intact
I have 5 web pages with different domain names.我有 5 个不同域名的网页。 Each of them uses the same directory and the same
.htaccess
file.他们每个人都使用相同的目录和相同的
.htaccess
文件。 I've redirected non www
urls to www
with this lines:我已使用以下行将
non www
网址重定向到www
:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Now I would like to keep redirecting non wwws to wwws, but I would also like to keep other subdomains without www.现在我想继续将非 www 重定向到 www,但我也想保留其他没有 www 的子域。 For example:
domain1.com
should redirect to www.domain1.com
and subdomain1.domain1.com
should still be subdomain1.domain1.com
and should not redirect to www.subdomain1.domain1.com
.例如:
domain1.com
应该重定向到www.domain1.com
并且subdomain1.domain1.com
应该仍然是subdomain1.domain1.com
并且不应该重定向到www.subdomain1.domain1.com
。
Keep in mind I have 5 webpages hosted from the same directory and there are more to come.请记住,我在同一目录中托管了 5 个网页,并且还会有更多网页。 I don't want to change
.htaccess
file everytime there is a page added.我不想在每次添加页面时更改
.htaccess
文件。
I don't want to mess up my .htaccess
file so I need your help!我不想弄乱我的
.htaccess
文件,所以我需要你的帮助!
PS: Is there a possibility to take https into account. PS:是否有可能将https考虑在内。
https://domain.com
to https://www.domain.com
http://domain.com
to http://www.domain.com
https://sub.domain.com
to https://sub.domain.com
http://sub.domain.com
to http://sub.domain.com
https://domain.com
到https://www.domain.com
http://domain.com
到http://www.domain.com
https://sub.domain.com
到https://sub.domain.com
http://sub.domain.com
到http://sub.domain.com
Thank you in advance for your answers!预先感谢您的回答!
Just target your rule for main domain:只需针对主域的规则:
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ [NC]
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS}s on(s)|
and %1
is used to maintain http/https
in target URL. %1
用于在目标 URL 中维护http/https
。
Make sure to clear your browser cache when testing this change.确保在测试此更改时清除浏览器缓存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.