htaccess的 - 如何强制" WWW"在通用的方式? [英] .htaccess - how to force "www." in a generic way?

查看:437
本文介绍了htaccess的 - 如何强制" WWW"在通用的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将改变 domain.com www.domain.com

# Force the "www."
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

我如何取代域的一部分,使这个工程上的任意的域名?

推荐答案

我会用这条规则:

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]

第一个条件检查是否主机的值不为空(在情况下的HTTP / 1.0);第二检查是否在主机的值不以 WWW 。对于HTTPS第三检查(%{HTTPS} 关闭,所以%{HTTPS}等附件取舍键,如果附件的取值匹配)。对重写规则然后就融合了信息部分完整的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屋!

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