5

我需要你的帮助。我想测试是否在没有 www 的情况下输入了 URL

喜欢example.com它应该转发到www.example.com

4

3 回答 3

12

试试这个 mod_rewrite 规则:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
于 2010-02-16T10:37:42.337 回答
2
RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^.*$ http://www.example.com/$0 [NC,L,R=301]
于 2010-02-16T10:37:13.190 回答
2

如果您正在使用nginx,则将此行添加到nginx config

server {
  listen 80;
  server_name yourdomain.com;
  rewrite ^/(.*) http://www.yourdomain.com/$1 permanent;
}
于 2011-10-21T08:25:56.350 回答