Apache 重定向(301)到新域名

文章
Apache 重定向(301)到新域名-下一朵云

Apache下301重定向代码(因为我使用的是LINUX + APACHE 所以本文仅限APACHE服务器使用。)

一、新建.htaccess文件,输入下列内容(需要开启mod_rewrite):

· 启用mod_rewrite模块
在conf目录的httpd.conf文件中找到
LoadModule rewrite_module modules/mod_rewrite.so
将这一行前面的#去掉。
· 在要支持url rewirte的目录启用
Options FollowSymLinks和AllowOverride All

1)将不带WWW的域名转向到带WWW的域名下

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^001.com [NC]
RewriteRule ^(.*)$ http://www.001.com/$1 [L,R=301]

2)重定向到新域名

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://www.001.com/$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.001.com
    DocumentRoot /home/fari001Com
</VirtualHost>

<VirtualHost *:80>
    ServerName fari001.com
    RedirectMatch permanent ^/(.*) http://www.001.com/$1
</VirtualHost>

其中开启HTTPS后,不带WWW域名转向WWW域名配置为:

<VirtualHost *:443>
    ServerName fari001.com
    RedirectMatch permanent ^/(.*) https://www.001.com/$1
</VirtualHost>

HTTP跳转HTTPS配置为:

<VirtualHost *:80>
    ServerName fari001.com
    RedirectMatch permanent ^/(.*) https://www.001.com/$1
</VirtualHost>