这里有很多关于如何将example.comredirect到www.example.com的示例,但是我正在寻找一个通用版本,所以相同的htacess代码也可以将example.netredirect到www.example.net和example.org到www.example.org 。 我确信这存在(事实上,我有一个模糊的记忆,我以前做过,在某个地方),但我很不熟悉htaccess的正则expression式。
我需要一种方法来匹配条件HTTP_HOST不以www.开头www. 。
#Enable RewriteEngine RewriteEngine On # For a way to check HTTP_HOST not beginning with www: RewriteCond %{HTTP_HOST} !^www [nc] # As ! don't set backreferences we use this to match the hostname we need RewriteCond %{HTTP_HOST} ^(.*)$ [nc] # For the Redirect itself: RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
看看这个问题在stackoverflow。
https://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www