Apache配置域名301跳转

配置域名301跳转

在有两个域名的情况下我们需要设置一个主域名(将次域名跳转到主域名下),操作命令如下:

vim /usr/local/apache2/conf/extra/httpd-vhosts.conf

在 # CustomLog "logs/dummy-host.example.com-access_log" common下面一行添加如下:

<IfModule mod_rewrite.c>

     RewriteEngine on

     RewriteCond %{HTTP_HOST} ^www.aaa.com$

     RewriteRule  ^/(.*)$  http://www.test.com/%1  [R=301,L]

</IfModule>

保存退出

#跳转301永久重定向   域名跳转一定要用301,对浏览器有好处

跳转302表示临时重定向

apachectl -M    #观察rewrite_module模块是否加载

apachectl -t

apachectl restart

curl -x192.168.1.113 :80 www.aaa.com/dsgsdgsdfg  -I  #测试域名是否正常跳转


不止有两个域名的情况操作如下:

vim /usr/local/apache2/conf/extra/httpd-vhosts.conf

添加:

i


RewriteCond %{HTTP_HOST} ^www.aaa.com$  [OR]

RewriteCond %{HTTP_HOST} ^www.bbb.com$

保存退出

apachectl -t

apachectl restart

curl -x192.168.1.113 :80 www.bbb.com/asllsdfsf  -I