Apache下301重定向代码
新建.htaccess文件,输入下列内容(需要开启mod_rewrite):
1)将不带WWW的域名转向到带WWW的域名下
- Options +FollowSymLinks
- RewriteEngine on
- RewriteCond %{HTTP_HOST} ^nbhao.org [NC]
- RewriteRule ^(.*)$ http://www.nbhao.org$1 [L,R=301]
2)重定向到新域名
- Options +FollowSymLinks
- RewriteEngine on
- RewriteRule ^(.*)$ http://www.nbhao.org/$1 [L,R=301]
3)使用正则进行301重定向,实现伪静态
- Options +FollowSymLinks
- RewriteEngine on
- RewriteRule ^news-(.+)\.html$ news.php?id=$1
将news.php?id=123这样的地址转向到news-123.html
Apache下vhosts.conf中配置301重定向
为实现URL规范化,SEO通常将不带WWW的域名转向到带WWW域名,vhosts.conf中配置为:
- <VirtualHost *:80>
- ServerName www.nbhao.org
- DocumentRoot /home/nbhao.org
- </VirtualHost>
- <VirtualHost *:80>
- ServerName nbhao.org
- RedirectMatch permanent ^/(.*) http://www.nbhao.org/$1
- </VirtualHost>