将之前使用的域名转向新域名,例如起点小说网最早使用的是www.cmfu.com,现在改成www.qidian.com;
由于老用户的存在只记住了www.cmfu.com,现在想输入老域名cmfu直接跳转到qidian.com上;
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/szl.com"
ServerName szl.com
ServerAlias www.example.com www.szl.com
<IfModule mod_rewrite.c> //需要mod_rewrite模块支持
RewriteEngine on //打开rewrite功能
RewriteCond %{HTTP_HOST} !^szl.com$ //定义rewrite的条件,如果主机名(域名)不是szl.com,那就满足条件
RewriteRule ^/(.*)$ http://szl.com/$1 [R=301,L] //定义rewrite规则,满足上面的条件,那么这条就生效
</IfModule>
ErrorLog "logs/szl.com-error_log"
CustomLog "logs/szl.com-access_log" common
</VirtualHost>
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
/usr/local/apache2.4/bin/apachectl -M |grep rewrite
vim /usr/local/apache2.4/conf/httpd.conf
取消注释LoadModule rewrite_module modules/mod_rewrite.so
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
使用IE浏览器测试,否则其他浏览器有可能在szl.com前面自动添加www
www.example.com/admin.php 成功跳转为http://szl.com/admin.php
[root@shu-test ~]# curl -x192.168.188.2:80 www.example.com/admin.php -I
HTTP/1.1 301 Moved Permanently
Date: Fri, 02 Mar 2018 10:10:25 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Location: http://szl.com/admin.php
Content-Type: text/html; charset=iso-8859-1
[root@shu-test ~]#
访问www.example.com 无论后缀的文件目录有多少一样跳转到szl.com下
[root@shu-test ~]# curl -x192.168.188.2:80 www.example.com/1dfkdsjldfj/ldsjfaos/sdlfs.php -I
HTTP/1.1 301 Moved Permanently
Date: Fri, 02 Mar 2018 10:11:39 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Location: http://szl.com/1dfkdsjldfj/ldsjfaos/sdlfs.php
Content-Type: text/html; charset=iso-8859-1
[root@shu-test ~]#
访问szl.com,不存在的文件目录错误将显示404
[root@shu-test ~]# curl -x192.168.188.2:80 szl.com/1dfkdsjldfj/lds -I
HTTP/1.1 404 Not Found
Date: Fri, 02 Mar 2018 10:13:54 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Content-Type: text/html; charset=iso-8859-1
[root@shu-test ~]#
访问szl.com,存在的文件目录错误将显示200
[root@shu-test ~]# curl -x192.168.188.2:80 szl.com/admin.php -I
HTTP/1.1 200 OK
Date: Fri, 02 Mar 2018 10:15:26 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
X-Powered-By: PHP/5.6.30
Content-Type: text/html; charset=UTF-8
[root@shu-test ~]#
原文地址:http://blog.51cto.com/shuzonglu/2082188