[英]htaccess force https and redirect www to non-www, but no other subdomains
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.在这一点上,我想知道是否有可能创建一个单一的 .htaccess 文件来完成我需要的一切,尽管我不得不承认我理解正则表达式,但我并不完全是一个 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.我的 SSL 证书涵盖 www-subdomain,所以我不是在寻找“不受信任的连接”错误解决方案,我知道这是不可能的。
I found most of the suggestions were not catching when you had something that was https://www.example.com and redirecting to https://example.com .我发现当您拥有https://www.example.com并重定向到https://example.com时,大多数建议都没有被捕获。
The following worked for all permutations:以下适用于所有排列:
RewriteEngine On
# match any URL with www and rewrite it to https without the www
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]
# match urls that are non https (without the www)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Hope this helps someone!希望这可以帮助某人!
Put this code in your DOCUMENT_ROOT/.htaccess
file:将此代码放在您的
DOCUMENT_ROOT/.htaccess
文件中:
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]
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.bewebdeveloper.com$
RewriteRule ^(.*) http://bewebdeveloper.com/$1 [QSA,L,R=301]
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]
This script will redirect此脚本将重定向
to到
while preserving the subdomain.同时保留子域。
Non of the examples worked for me, they just seemed to get stuck in a loop, the following works though.没有一个例子对我有用,它们似乎陷入了一个循环,但下面的方法有效。
# match any URL with www and rewrite it to https without the www
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [R=301,L]
# match non https and redirect to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
The order matters, it will prevent a 3rd redirect in some cases.顺序很重要,在某些情况下它会阻止第三次重定向。
If you want to use a sub domain (anything other than www.) just remove the first ruleset.如果您想使用子域(除 www. 之外的任何域),只需删除第一个规则集。
So for sub domains all you need is因此,对于子域,您只需要
# match non https and redirect to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
I use Cloudways services and I've found this to be the only thing that works我使用 Cloudways 服务,我发现这是唯一有效的方法
I found the following answer would match to your requirement:我发现以下答案符合您的要求:
www to non-www with https but no other subdomains
www到非 www与https但没有其他子域
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Fulfill all of the three (3) conditions:满足所有三 (3) 个条件:
(1) http://www.example.com/ -> https://example.com/
(2) http://example.com/ -> https://example.com/
(3) https://www.example.com -> https://example.com/
but no other subdomains than www
like so:但除了
www
没有其他子域,像这样:
(4) http://others.example.com -> https://others.example.com/
(5) https://others.example.com -> https://others.example.com/
just add this code in root .htaccess file只需将此代码添加到根 .htaccess 文件中
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.