域名跳转需要在虚拟主机配置中添加别名和一个 rewrite 模块,如下,配置当访问 www.aaa.com 时跳转到 www.test.com
[[email protected] ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/data/www" ServerName www.test.com ServerAlias www.aaa.com <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} ^www.aaa.com$ RewriteRule ^/(.*)$ http://www.test.com/$1 [R=301,L] </IfModule> </VirtualHost>
[[email protected] ~]# /usr/local/apache2/bin/apachectl -t [[email protected] ~]# /usr/local/apache2/bin/apachectl graceful
扩展:如果有多个域名跳转到一个域名如何配置,如下,配置当访问 www.aaa.com 或访问 www.bbb.com 时跳转到 www.test.com
[[email protected] ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/data/www" ServerName www.test.com ServerAlias www.aaa.com # 这里配置两个别名 ServerAlias www.bbb.com <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} ^www.aaa.com$ [OR] # 这里末尾要加[OR],表示或者 RewriteCond %{HTTP_HOST} ^www.bbb.com$ RewriteRule ^/(.*)$ http://www.test.com/$1 [R=301,L] </IfModule> </VirtualHost>
时间: 2024-10-17 19:56:16