htaccess的重定向非www HTTP和HTTPS [英] htaccess redirect for non-www both http and https

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

问题描述

我想有:

  • http://example.com 重定向到: http://www.example.com
  • https://example.com 重定向到: https://www.example.com
  • http://example.com redirect to: http://www.example.com
  • https://example.com redirect to: https://www.example.com

和任何的 http://whatever.example.com 不追加了www喜欢的http://www.whatever.example。 COM

And anything that is http://whatever.example.com NOT append the www like http://www.whatever.example.com.

推荐答案

试试这个规则:

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

下面是一个解释:

  1. 在第一个条件测试,如果HTTP头字段的主机的具有要求的格式(只包含一个周期)。
  2. 第二个条件测试,如果值的串联值的 HTTPS 的变量(值关闭)和取值(所以无论是附件取舍)等于附件并捕获取值。这意味着,如果%{HTTPS}等的计算结果为附件,第一个匹配的组取值空,否则。
  3. 在该规则将匹配所有的请求,因为每个字符串有一个开始(标有 ^ ),并将其重定向到 HTTP%1的评估值://www.% {HTTP_HOST}%{REQUEST_URI} 如果这两个条件都满足。其中,%1 是第一个匹配的组的previous条件(取值如果HTTPS和空否则) %{HTTP_HOST} 是HTTP的主机的要求和%{REQUEST_URI} 是被请求的绝对URL路径。
  1. The first condition tests if the HTTP header field Host has the required format (contains exactly one period).
  2. The second condition tests if the concatenated value of the value of the HTTPS variable (values on and off) and s (so either ons or offs) is equal to ons and captures the s. This means if %{HTTPS}s evaluates to ons, the first matching group is s and empty otherwise.
  3. The rule will match all requests as every string has a start (marked with ^) and redirects them to the evaluated value of http%1://www.%{HTTP_HOST}%{REQUEST_URI} if both conditions are true. Where %1 is the first matching group of the previous condition (s if HTTPS and empty otherwise), %{HTTP_HOST} is the HTTP Host of the request and %{REQUEST_URI} is the absolute URL path that was requested.

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

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