强制www在htaccess中除了所有子域名

时间:2013-12-17 09:57:35

标签: apache .htaccess subdomain

我的力量www htaccess代码是:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

但我对子域名 ex:

有疑问
abcdef.example.com  ====> www.abcdef.example.com

我想编辑像这样的代码:

example.com         ===> www.example.com
abcdefg.example.com ===> abcdefg.example.com
example.co.uk       ===> www.example.co.uk
abcd.example.co.uk  ===> abcd.example.co.uk

1 个答案:

答案 0 :(得分:5)

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.(co\.uk|com)
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

或更通用:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[0-9a-zA-Z-]+\.[a-zA-Z]{2,}$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]