如何一次跳转所有请求到https和非www? [英] how can I Redirect all requests to https and non-www in one jump?

查看:109
本文介绍了如何一次跳转所有请求到https和非www?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一次将所有请求重定向到https和非www的主页/主页和其他子页面。
我正在使用以下htaccess。



我要这样:

  http://mytesting.com> https://mytesting.com 
http://www.mytesting.com> https://mytesting.com
http://mytesting.com/faq> https://mytesting.com/faq
https://mytesting.com> https://mytesting.com
http://www.mytesting.com> https://mytesting.com
http://www.mytesting.com/faq> https://mytesting.com/faq
https://mytesting.com/faq> https://mytesting.com/faq


解决方案

您可以在一个规则中将这条规则用于两个重定向:

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

此规则的解释:




  • RewriteCond%{HTTP_HOST} ^ www\。 [NC,OR] :如果 HOST_NAME www开头。

  • [NC,OR] :忽略大小写匹配和 OR 的下一个条件

  • RewriteCond%{HTTPS}!on HTTPS 未打开

  • RewriteCond%{HTTP_HOST} ^(?: www\。)?(。+)$ [NC] :此条件始终匹配,因为 www。是此处的可选匹配项。它用于捕获 HTTP_HOST 的子字符串,而无需使用(。+)启动 www。。捕获组#1中的模式(以后将被反向引用为%1 )。请注意,(?:..)是一个非捕获组。

  • RewriteRule ^ https:/ /%1%{REQUEST_URI} [R = 301,L,NE] ^ 将始终匹配。该规则将使用 R = 301 代码重定向到 https://%1%{REQUEST_URI} 。如上所述,其中%1 是来自 RewriteCond 的捕获组#1的反向引用。


I want to redirect all requests to https and non-www in one jump for main/ home page and other sub pages. I am using the following htaccess. source

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^mytesting\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mytesting\.com$
RewriteRule .* https://www.mytesting.com%{REQUEST_URI} [R=301,L]

But I got the following redirection

I want like this:

http://mytesting.com          > https://mytesting.com
http://www.mytesting.com      > https://mytesting.com
http://mytesting.com/faq      > https://mytesting.com/faq
https://mytesting.com         > https://mytesting.com
http://www.mytesting.com      > https://mytesting.com
http://www.mytesting.com/faq  > https://mytesting.com/faq
https://mytesting.com/faq     > https://mytesting.com/faq

解决方案

You may use this rule for both redirects in one rule:

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

Here is the explanation of this rule:

  • RewriteCond %{HTTP_HOST} ^www\. [NC,OR]: if HOST_NAME starts with www.
  • [NC,OR]: Ignore case match and ORs next condition
  • RewriteCond %{HTTPS} !on: HTTPS is not turned on
  • RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]: This condition will always match since www. is an optional match here. It is used to capture substring of HTTP_HOST without starting www. by using (.+) pattern in capture group #1 (to be back-referenced as %1 later). Note that (?:..) is a non-capturing group.
  • RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]: ^ will always match. This rule will redirect to https://%1%{REQUEST_URI} with R=301 code. Where %1 is back-reference of capture group #1 from RewriteCond, as mentioned above.

这篇关于如何一次跳转所有请求到https和非www?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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