.htaccess:HTTP到HTTPS重定向不起作用

时间:2021-10-24 11:18:13

I want to redirect: 1) http://example.com to https://www.example.com 2) http://www.example.com to https://www.example.com

我想重定向:1)http://example.com到https://www.example.com 2)http://www.example.com到https://www.example.com

But my .htaccess usually ignores %{HTTPS} in RewriteCond... Hence I am not able to handle the second mentioned case, it gives me cyclic redirection error message.

但我的.htaccess通常忽略RewriteCond中的%{HTTPS} ...因此我无法处理第二个提到的情况,它给了我循环重定向错误消息。

.htaccess is as follows:

.htaccess如下:

# Redirect non-www urls to www
RewriteEngine on

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.example\.com/?$ 
RewriteRule ^(.*)$ https://www.example.com [NC,R=301]

RewriteCond %{HTTP_HOST} ^example\.com/?$ 
RewriteRule ^(.*)$ https://www.example.com [NC,R=301]

RewriteCond %{HTTP_HOST} ^example\.com\?.*$
RewriteRule ^(.*)$ https://www.example.com/$1 [NC,R=301]

I will try to say very nice thank you! to anybody who will help me to solve this.

我会尽力说非常好谢谢!任何帮助我解决这个问题的人。

1 个解决方案

#1


You really only need one rule for what you want.

你真的只需要一个你想要的规则。

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC,OR] 
RewriteCond %{HTTPS} !^on
RewriteRule ^(.*)$ https://www.example.com [NC,R=301,L]

#1


You really only need one rule for what you want.

你真的只需要一个你想要的规则。

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC,OR] 
RewriteCond %{HTTPS} !^on
RewriteRule ^(.*)$ https://www.example.com [NC,R=301,L]