使用一个301重定向重定向到https和www [英] Redirect to https and www with one 301 redirect

查看:84
本文介绍了使用一个301重定向重定向到https和www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

我想将所有请求重定向到 https://www URL,例如

I want to redirect all requests to land on https://www URL, e.g.

case1: http://www.example.com --301--> https://www.example.com
case2: https://example.com    --301--> https://www.example.com
case3: http://example.com     --301--> https://www.example.com

我的代码在.htaccess文件中

Here I have my code in .htaccess file

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

在情况1和2下工作正常.但是,当我有一个http和非www请求(情况3)时,它将需要两个301重定向才能到达

It works fine in case 1 and 2. However when I have a http and non-www request (case3), it will require two 301 redirects to land on https://www

http://example.com 
--301--> 
https://example.com 
--301--> 
https://www.example.com

我们如何强制始终只有1 301重定向的https和www.

How can we force https and www with always just 1 301 redirect.

我对Apache和重写模块不是很熟悉.请帮忙!

I am not very familiar with Apache and rewrite module. Help please!

推荐答案

一次即可重定向到 https://www 请求,您可以使用:

To redirect to https://www in a single request, you can use :

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
 RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R=301]

这篇关于使用一个301重定向重定向到https和www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆