致力于为用户提供真实的
主机测评数据及优惠信息

Apache中301重定向的配置代码

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

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

复制代码 代码如下:

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

2)重定向到新域名

复制代码 代码如下:

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

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

Apache下除了以上2种方法,还有其他配置方法和可选参数,建议阅读Apache文档。

赞(0) 打赏
未经允许不得转载:爱主机 » Apache中301重定向的配置代码
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址