设置.htaccess文件:
 
使访问example.com的时候就会自动转到www.example.com 
在.htaccess文件里写上以下代码即可。
 
 
 
RewriteEngine On
 
RewriteCond %{http_host} ^mydomain.com [NC]
 
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
 
 
 
 
 
如果是多条301重定向就需要写多行,例如想让verydz.com和www.verydz.net 都跳转到www.verydz.com,可以这样写:
 
RewriteEngine On
 
RewriteCond %{http_host} ^verydz.com [NC]
 
RewriteRule ^(.*)$ http://www.verydz.com/$1 [L,R=301]
 
RewriteCond %{http_host} ^www.verydz.net[NC]
 
RewriteRule ^(.*)$ http://www.verydz.com/$1 [L,R=301]
 
复制代码简化写法:
RewriteEngine On
 
RewriteCond %{http_host} !^www.verydz.com [NC]
 
RewriteRule ^(.*)$ http://www.verydz.com/$1 [L,R=301]
 
 
 
 
 
注意:URL标准化的301重定向(以上代码)需要写在其他URL-rewrite代码之前。
RewriteEngine on只能出现一次,如果已经有了就不用重复写了,这个要写在第一行。
 
例如:已经有了Discuz!伪静态代码,还需要加301重定向代码写法如下:
 
# 将 RewriteEngine 模式打开
 
RewriteEngine On
 
RewriteCond %{http_host} ^mydomain.com [NC]
 
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
 
# 修改以下语句中的 /discuz 为您的论坛目录地址,如果程序放在根目录中,请将 /discuz 修改为 /
 
RewriteBase /
 
# Rewrite 系统规则请勿修改
 
RewriteCond %{QUERY_STRING} ^(.*)$
 
RewriteRule ^topic-(.+)\.html$ portal.php?mod=topic&topic=$1&%1
 
RewriteCond %{QUERY_STRING} ^(.*)$
 
RewriteRule ^article-([0-9]+)-([0-9]+)\.html$ portal.php?mod=view&aid=$1&page=$2&%1
 
RewriteCond %{QUERY_STRING} ^(.*)$
 
RewriteRule ^forum-(\w+)-([0-9]+)\.html$ forum.php?mod=forumdisplay&fid=$1&page=$2&%1
 
RewriteCond %{QUERY_STRING} ^(.*)$
 
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
 
RewriteCond %{QUERY_STRING} ^(.*)$
 
RewriteRule ^group-([0-9]+)-([0-9]+)\.html$ forum.php?mod=group&fid=$1&page=$2&%1
 
RewriteCond %{QUERY_STRING} ^(.*)$
 
RewriteRule ^space-(username|uid)-(.+)\.html$ home.php?mod=space&$1=$2&%1
 
RewriteCond %{QUERY_STRING} ^(.*)$
 
RewriteRule ^blog-([0-9]+)-([0-9]+)\.html$ home.php?mod=space&uid=$1&do=blog&id=$2&%1
 
RewriteCond %{QUERY_STRING} ^(.*)$
 
RewriteRule ^archiver/(fid|tid)-([0-9]+)\.html$ archiver/index.php?action=$1&value=$2&%1
 
RewriteCond %{QUERY_STRING} ^(.*)$
 
RewriteRule ^([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ plugin.php?id=$1:$2&%1