当公司的网站更新了新的站点域名,希望原有的域名依旧能够提供服务,这时就需要使用域名跳转。用户访问原有域名时,自动跳转到新的域名,访问新域名所对应的网站页面。
1、修改虚拟主机配置
[root@juispan ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/data/www/abc.com" ServerName abc.com <IfModule mod_rewrite.c> ##需要mod_rewrite模块支持 RewriteEngine on ##打开rewrite功能 RewriteCond %{HTTP_HOST} !^abc.com$ ##定义rewrite的条件 RewriteRule ^(/.*)$ http://www.baidu.com/$1 [R=301,L] ##定义rewrite规则 </IfModule> </VirtualHost>
2、检查重新加载
[root@juispan ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@juispan ~]# /usr/local/apache2.4/bin/apachectl graceful [root@juispan ~]# /usr/local/apache2.4/bin/apachectl -M | grep -i rewrite [root@juispan ~]# vi /usr/local/apache2.4/conf/httpd.conf LoadModule rewrite_module modules/mod_rewrite.so ##解除注释启用 [root@juispan ~]# /usr/local/apache2.4/bin/apachectl -M | grep -i rewrite rewrite_module (shared)
3、验证测试
[root@juispan ~]# curl -x127.0.0.1:80 abc.com -I ##-I:不显示站内内容,只显示状态码 HTTP/1.1 200 OK Date: Fri, 21 Jul 2017 09:34:55 GMT Server: Apache/2.4.27 (Unix) PHP/7.1.6 X-Powered-By: PHP/7.1.6 Content-Type: text/html; charset=UTF-8 [root@juispan ~]# curl -x127.0.0.1:80 abcd.com -I HTTP/1.1 301 Moved Permanently ##301 跳转 Date: Fri, 21 Jul 2017 09:35:01 GMT Server: Apache/2.4.27 (Unix) PHP/7.1.6 Location: http://abc.com/ Content-Type: text/html; charset=iso-8859-1 [root@juispan ~]# curl -x127.0.0.1:80 abcd.com/123.php -I HTTP/1.1 301 Moved Permanently Date: Fri, 21 Jul 2017 09:39:00 GMT Server: Apache/2.4.27 (Unix) PHP/7.1.6 Location: http://abc.com/123.php Content-Type: text/html; charset=iso-8859-1