重定向到HTTP非www到HTTPS www htaccess

时间:2013-07-03 16:31:46

标签: .htaccess

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

  • http://www.site.cohttps://www.site.co
  • http://site.cohttps://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网站。

13 个答案:

答案 0 :(得分:42)

试试这样:

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 :(得分:22)

Prix的答案有效。为了使其更具动态性,我们可以使用SERVER_NAME和REQUEST_URI代替静态域名。

 $scope.$watch(function () {
     return $scope.sortExpression
 }, function (newSort) {
     $scope.sortBy = 'data.' + $scope.sortExpression
 })

 $http.get("https://www.reddit.com/r/worldnews/new.json")
     .success(function (response) {
     $scope.names = response.data.children
 })

答案 2 :(得分:2)

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

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

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

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

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] 

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

{{1}}

答案 3 :(得分:2)

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

如果您想重定向到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>

如果要使用展位功能,请分别放置以上所有代码

答案 4 :(得分: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]

答案 5 :(得分:1)

尝试以下,工作正常

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

答案 6 :(得分:1)

我有一个asp.net网站,并且主机提供商不支持IIS重写模块,我已经测试了所有这些方法,并且只有这段代码可以给woorank.com确认,这可能对另一个.net开发人员有用:

analysis_options.yaml

答案 7 :(得分:0)

这将用于www或非www 如果您尝试使用www打开链接,则网址会重定向到https with 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]

答案 8 :(得分:0)

只需将以下内容放入.htaccess文件

 RewriteEngine on

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

答案 9 :(得分:0)

我用这个:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [END,NE,R=permanent]

答案 10 :(得分:0)

尝试此方法:

# Redirect 301 all to https://www
<IfModule mod_rewrite.c>
   RewriteCond %{HTTPS} off [OR]
   RewriteCond %{HTTP_HOST} !^www\. [NC]
   RewriteCond %{HTTP_HOST} ^(.*)$  [NC]
   RewriteRule (.*) https://www.example.com/$1 [L,R=301]
</IfModule>

为什么有时您不需要代码%{HTTP_HOST} 并在域名的最后一行中使用带www的域名确切网址,因为通常结果中会有两个www,例如:

https://www.www.yourdomain.com

如果使用cloudflare,只需创建页面规则将301全部重定向到https和www版本。

创建页面规则 填充

example.com/*

选择设置转发网址,然后选择301并填写

https://www.example.com/$1

保存并全部重定向到https和www版本。

答案 11 :(得分:-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] 

答案 12 :(得分:-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更改为您自己的域。

相关问题