简体   繁体   English

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

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

I want to redirect my domain http to https and I have already do that with below mention code 我想将我的域名http重定向到https,并且已经使用下面提到的代码进行了此操作

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

When a user enter domain.com it's automatically redirect to https www.domain.com But when user enter only www.domain.com it's redirect to the http www.domain.com 当用户输入domain.com时,它将自动重定向到https www.domain.com,但是当用户仅输入www.domain.com时,它将重定向到http www.domain.com

Please help me to solve this issue. 请帮我解决这个问题。

You can use: 您可以使用:

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]

At your code : 在您的代码:

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

It is a condition for the following rule ; 这是遵循以下规则的条件; so the rule will be applied only for none www request. 因此,该规则仅适用于所有www请求。

Moreover , don't use R=301 redirection when you test new code unless you make sure that your rules is ok and you could use R=302 or just R . 此外,不要使用R=301重定向当你测试新的代码,除非你确保你的规则是确定的,你可以使用R=302或只R

regardless of your code Put the following code at your main directory .htaccess : 无论您使用什么代码,请将以下代码放在主目录.htaccess

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

If you want to force every request to be www you should do this : 如果要强制将每个请求都设置为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