2

I was using this code to remove www from my domain:

# remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

My problem is that I usually use subdomains and it doesn't work for them. (www.sub.example.com).

How do I rewrite to remove www whatever the domain is and even if it has subdomains?

4

2 回答 2

1

尝试这个:

#********** Remove www from address **********
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC]

这比其他解决方案更正确。

编辑:我删除了 [L] 因为它在这里不适用。
尽管在大多数情况下,建议使用不区分大小写的 [NC]。

于 2012-08-20T12:05:49.413 回答
0

您可能可以使用捕获组并结合重写......(完全未经测试的猜测)

# remove www
RewriteCond %{HTTP_HOST} ^www.(.*) [NC]
RewriteRule ^(.*)$ http://$1 [R=301,L]
于 2012-07-01T01:02:57.267 回答