在没有www的情况下将重写规则设为域

时间:2013-07-03 12:38:03

标签: regex .htaccess url-rewriting

我想将http://www.example.net/anything重写为http://example.net/anything。但我正在寻找适用于多个不同领域的普遍规则。

我做了这个,但它不起作用

RewriteCond %{HTTP_HOST} www\.(.+)$
RewriteRule www\.(.+)$ http://$1 [R=301]

2 个答案:

答案 0 :(得分:1)

(.+)中的RewriteCond捕获会将值存储到%1,这就是RewriteRule中您需要的内容。 www(或域的任何部分)不会出现在值RewriteRule进程中:

# Capture the domain without www into %1
RewriteCond %{HTTP_HOST} ^www\.(.+)$
# Rewrite the whole URI to the %1 domain
RewriteRule (.*) http://%1/$1 [L,R=301]

答案 1 :(得分:1)

RewriteRule在url Path(不包括域名)上运行:

RewriteCond %{HTTP_HOST} www\.(.+)$
RewriteRule (.*) http://%1/$1 [R=301]
相关问题