以下是我已经尝试过的解决方案:通过 htaccess 强制非 www 和 https有点像,但似乎会产生重定向循环.htaccess 使用 SSL 将 www 重定向到非 www/HTTPS适用于 (1) 和 (3),但不适用于 (2)htaccess 强制 www 为非-www 考虑到 http 或 https针对 (1) 重定向到 http://mydomain.com/(不带 https)对 (2) 没有任何作用针对 (3) 重定向到 http://mydomain.com/(不带 https)我的 SSL 证书涵盖 www-subdomain,所以我不是在寻找不受信任的连接"错误解决方案,我知道这是不可能的. 解决方案 将此代码放入您的 DOCUMENT_ROOT/.htaccess 文件中:RewriteEngine OnRewriteCond %{HTTPS} 关闭RewriteCond %{ENV:HTTPS} 关闭重写规则 ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]重写规则 ^ https://%1%{REQUEST_URI} [L,R=301,NE]I know there are many similar threads, but none of them seems to match my exact problem. Here is what I’m trying to do:(1) http://www.mydomain.com/ -> https://mydomain.com/(2) http://mydomain.com/ -> https://mydomain.com/(3) https://www.mydomain.com -> https://mydomain.com/Ideally, I would also like to cover the situation if I will ever add more subdomains, to automatically behave like the following (preferably in a generic way that will work for any subdomain I add).(4) http://sub.mydomain.com/ -> https://sub.mydomain.com/At this point I’m wondering if it is even possible to create a single .htaccess file that does everything I need, although I have to admit that I understand regexes, but I’m not exactly a mod_rewrite uberpro.Here are the solutions that I already tried:Force non-www and https via htaccessSort of works, but seems to generate a redirect loop.htaccess redirect www to non-www with SSL/HTTPSWorks for (1) and (3), but not for (2)htaccess force www to non-www with consideration of http or httpsRedirects to http://mydomain.com/ (without https) for (1)Does nothing for (2)Redirects to http://mydomain.com/ (without https) for (3)My SSL certificate covers the www-subdomain, so I’m not looking for a 'untrusted connection' error solution, I’m aware that isn’t possible. 解决方案 Put this code in your DOCUMENT_ROOT/.htaccess file:RewriteEngine OnRewriteCond %{HTTPS} offRewriteCond %{ENV:HTTPS} offRewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE] 这篇关于htaccess 强制 https 并将 www 重定向到非 www,但没有其他子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 01:40
查看更多