本文介绍了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
都不会像 http://www.whatever.example.com
那样附加 www.
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]
解释如下:
- 第一个条件测试 HTTP 标头字段 Host 是否具有所需的格式(正好包含一个句点).
- 第二个条件测试 HTTPS 变量的值(值
on
和off
)和s 的连接值
(所以ons
或offs
)等于ons
并捕获s
.这意味着如果%{HTTPS}s
的计算结果为ons
,则第一个匹配组为s
,否则为空. - 该规则将匹配所有请求,因为每个字符串都有一个开头(用
^
标记)并将它们重定向到http%1://www.%{HTTP_HOST} 的评估值%{REQUEST_URI}
如果两个条件都为真.其中%1
是前一个条件的第一个匹配组(s
如果为 HTTPS,否则为空),%{HTTP_HOST}
是 HTTP 请求的主机,%{REQUEST_URI}
是请求的绝对 URL 路径.
- The first condition tests if the HTTP header field Host has the required format (contains exactly one period).
- The second condition tests if the concatenated value of the value of the HTTPS variable (values
on
andoff
) ands
(so eitherons
oroffs
) is equal toons
and captures thes
. This means if%{HTTPS}s
evaluates toons
, the first matching group iss
and empty otherwise. - The rule will match all requests as every string has a start (marked with
^
) and redirects them to the evaluated value ofhttp%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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!