简体   繁体   English

将非 www 重定向到 www,但保持其他子域完好无损

[英]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.comhttps://www.domain.com http://domain.comhttp://www.domain.com https://sub.domain.comhttps://sub.domain.com http://sub.domain.comhttp://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.

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