htaccess 强制 https 并将 www 重定向到非 www,但没有其他子域 [英] htaccess force https and redirect www to non-www, but no other subdomains
问题描述
我知道有很多类似的主题,但似乎没有一个与我的确切问题相符.这是我想要做的:
(1) http://www.mydomain.com/->https://mydomain.com/(2) http://mydomain.com/->https://mydomain.com/(3) https://www.mydomain.com ->https://mydomain.com/
理想情况下,如果我将添加更多子域,我还想涵盖这种情况,以自动执行如下操作(最好采用适用于我添加的任何子域的通用方式).
(4) http://sub.mydomain.com/->https://sub.mydomain.com/
此时我想知道是否有可能创建一个单一的 .htaccess 文件来完成我需要的一切,尽管我不得不承认我理解正则表达式,但我并不完全是一个 mod_rewrite uberpro.>
以下是我已经尝试过的解决方案:
- 通过 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 htaccess
- Sort of works, but seems to generate a redirect loop
- .htaccess redirect www to non-www with SSL/HTTPS
- Works for (1) and (3), but not for (2)
- htaccess force www to non-www with consideration of http or https
- Redirects 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 On
RewriteCond %{HTTPS} off
RewriteCond %{ENV:HTTPS} off
RewriteRule ^ 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,但没有其他子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!