域名跳转总结

当我们变更网站域名,或者申请多个域名指向一个网站的时候,这个时候我们就会用到域名跳转(或者叫域名重定向redirect、域名转向)。

域名跳转试验

[root@chy ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName www.111.com
    ServerAlias www.exaple.com
    #<Directory /data/wwwroot/111.com>
     #<filesMatch 123.php>
      #AllowOverride AuthConfig
      #AuthName "111.com user auth"
        #AuthType Basic
       # AuthUserFile /data/.htpasswd
      #  require valid-user
     # </FilesMatch>
      #</Directory>
     <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{HTTP_HOST} !^www.123.com$
        RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L]
</IfModule>
    #ErrorLog "logs/dummy-host2.example.com-error_log"
    #CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>
(以上是配置,以下是具体的配置说明:
<IfModule mod_rewrite.c> //需要mod_rewrite模块支持
        RewriteEngine on  //打开rewrite功能
        RewriteCond %{HTTP_HOST} !^www.111.com$  //定义rewrite的条件,主机名(域名)不是www.111.com满足条件
        RewriteRule ^/(.*)$ http://www.111.com/$1 [R=301,L] //定义rewrite规则,当满足上面的条件时,这条规则才会执行,其中301是永久跳转,302是只跳转一次。L的意思是跳转一次后就结束
</IfModule>
[root@chy ~]# /usr/local/apache2.4/bin/apachectl -M |grep rewrite
(查看是否加载了模块)
[root@chy ~]# vi /usr/local/apache2.4/conf/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so
(在主配置文件里面找到rewrite_module模块将#号去掉,然后在查看是否有模块)
[root@chy ~]# curl -x127.0.0.1:80 222.com -I
HTTP/1.1 301 Moved Permanently
Date: Sun, 30 Jul 2017 23:17:17 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
Location: http://111.com/
Content-Type: text/html; charset=iso-8859-1
(测试域名跳转成功)

以上测试是在centos7系统下完成