htaccess 强制 https 并将 www 重定向到非 www,但没有其他子域 [英] htaccess force https and redirect www to non-www, but no other subdomains

查看:30
本文介绍了htaccess 强制 https 并将 www 重定向到非 www,但没有其他子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多类似的主题,但似乎没有一个与我的确切问题相符.这是我想要做的:

(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.

以下是我已经尝试过的解决方案:

我的 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:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆