繁体   English   中英

如何将domain.com或www.domain.com重定向到https www.domain.com

[英]How to redirect domain.com or www.domain.com to https www.domain.com

我想将我的域名http重定向到https,并且已经使用下面提到的代码进行了此操作

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

当用户输入domain.com时,它将自动重定向到https www.domain.com,但是当用户仅输入www.domain.com时,它将重定向到http www.domain.com

请帮我解决这个问题。

您可以使用:

RewriteEngine on
# Test without www
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
# Test for http
RewriteCond %{HTTPS} off 
# Redirect to https www
RewriteRule ^ https://www.exemple.com%{REQUEST_URI} [NE,R=301,L]

在您的代码:

RewriteCond %{HTTP_HOST} ^domain.com [NC]

这是遵循以下规则的条件; 因此,该规则仅适用于所有www请求。

此外,不要使用R=301重定向当你测试新的代码,除非你确保你的规则是确定的,你可以使用R=302或只R

无论您使用什么代码,请将以下代码放在主目录.htaccess

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

如果要强制将每个请求都设置为www ,则应执行以下操作:

RewriteEngine On

RewriteCond %{HTTPS} !=on
#this line above will match any request without `https`

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

#the two lines above will exclude none `www` request and force only `www` request unto `https` and the rule will stop here `L`

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]

# the last two line will exclude any `www` request and force only none `www` into `https`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM