.htaccess - 如何强制“www."以一种通用的方式? [英] .htaccess - how to force "www." in a generic way?
问题描述
这会将 domain.com
更改为 www.domain.com
:
# Force the "www."
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
如何替换域"部分,使其适用于任何域?
How do I replace the "domain" part so that this works on any domain?
推荐答案
我会使用这个规则:
RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
第一个条件检查 Host 值是否不为空(在 HTTP/1.0 的情况下);第二个检查 Host 值是否不以 www.
开头;第三次检查 HTTPS(%{HTTPS}
是 on
或 off
,所以 %{HTTPS}s
是 ons
或 offs
并且在 ons
的情况下 s
匹配).RewriteRule
的替换部分然后只是将信息部分合并为一个完整的 URL.
The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with www.
; the third checks for HTTPS (%{HTTPS}
is either on
or off
, so %{HTTPS}s
is either ons
or offs
and in case of ons
the s
is matched). The substitution part of RewriteRule
then just merges the information parts to a full URL.
这篇关于.htaccess - 如何强制“www."以一种通用的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!