重写通配符子域和通配符域

对于一个托pipe域名的networking服务器,我们希望te实现一个redirect,任何域上的任何子域都会redirect到它的www。 当量。

sub1.domain1.com -> www.domain1.com sub2.domain5.com -> www.domain5.com anothersub.moredomains.com -> www.moredomains.com etc 

我一直无法find覆盖通配符子域和域的重写规则。

任何帮助是极大的赞赏!

如果域总是有两个组件(x.tld),那么这很容易。 如果你不得不考虑像x.co.uk这样的事情,那么你将需要一个更大的规则或​​实际的域名列表。

没有testing,但是这样的事情可能会起作用。

 # enable mod_rewrite RewriteEngine On # if the host is prefixed with www, don't match. RewriteCond %{HTTP_HOST} !^www\. # match the last two components of the hostname (see below for a bette regex) RewriteCond %{HTTP_HOST} (.+?)\.(.+?)$ # rewrite to the hostname from the match above (%1 and %2) plus the current url ($1). RewriteRule /(.*) http://www.%1.%2/$1 [L,R=301] 

 LogLevel alert rewrite:trace 

你可以在错误日志中grep for'rewrite:'来获得debugging信息。

(.+?)条件可能更准确,因为“任何东西,但点” ([^\.]+) ,但我不记得在Apache使用任何正则expression式引擎的确切语法,所以上述版本更简单你有一些工作。