重定向到HTTP非www到HTTPS www htaccess

问题描述 投票:15回答:11

我想使用HTTPS协议从任何方向重定向到我们的网站,但有些重定向它不起作用。我要这个:

  • http://www.site.co to https://www.site.co
  • http://site.co to https://www.site.co

这是我的htaccess:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301] 

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

第二条规则不起作用。它会转到我们网站内的另一个方向,而不会重定向到HTTPS网站。

.htaccess
11个回答
44
投票

试试这样:

RewriteEngine On

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

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

这里唯一真正的区别是,首先我们从非WWW重定向到WWW然后我们检查HTTPS并重定向它。

如果它不起作用,试试这个:

RewriteEngine On

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

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301] 

-1
投票
RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301] 

-1
投票

例如https://example.com应该去https://www.example.com

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

注意 - 您应该将example.com更改为您自己的域。


24
投票

Prix​​的答案有效。要使其更具动态性,请使用SERVER_NAME和REQUEST_URI而不是静态域名。

RewriteEngine On
#we replace domain.com/$1 with %{SERVER_NAME}%{REQUEST_URI}.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

#here we dont use www as non www was already redirected to www.
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

3
投票

抱歉,我没有足够的评论意见,但@prix的规则会带来不必要的重定向。

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301] 

您可以在GTMETRIX上尝试http://domain.com/,您将收到此消息

 "Avoid landing page redirects for the following chain of redirected URLs."

http://domain.com/
http://www.domain.com/
https://www.domain.com/

要防止这种情况,请转到第一个RewriteRule并在http的末尾添加“s”。新规则将如下所示:

RewriteEngine On

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

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301] 

2
投票

这是使用.htaccess的http到https和非www到www重定向

如果您想重定向到https和www,您可以使用此代码

http到https重定向:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
</IfModule>

非www到www重定向:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

如果要使用booth功能,请分别放置上述所有代码


1
投票

如果您使用One.com作为您的虚拟主机,则应使用以下代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

1
投票

尝试以下,工作正常

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com$1 [R,L]

0
投票

这将用于www或非www如果您尝试打开与www的链接然后url重定向到https与www

Example : http://domain.com redirect to https://domain.com

或者如果您尝试打开与非www的链接,则url重定向到非www的https

Example : http://www.domain.com redirect to https://www.domain.com

RewriteEngine on

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

0
投票

只需将以下内容放在.htaccess文件中即可

 RewriteEngine on

 RewriteCond %{SERVER_PORT} 80 
 RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

0
投票

我用这个:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [END,NE,R=permanent]
© www.soinside.com 2019 - 2024. All rights reserved.