域名跳转
域名跳转也叫做301重定向,当我们变更网站域名,或者申请多个域名指向一个网站的时候,这个时候我们就会用到域名跳转(或者叫域名重定向redirect、域名转向)
几个特征码:
401 访问认证
403 不允许访问(配置文件能设置)
404 页面不存在
301 永久重定向
302 临时重定向
200 正确
域名跳转设置:
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com
<IfModule mod_rewrite.c> //需要mod_rewrite模块支持
RewriteEngine on //打开rewrite功能
RewriteCond %{HTTP_HOST} !^www.123.com$ //定义rewrite的条件,主机名(域名)不是www.123.com满足条件就跳转
RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L] //定义rewrite规则,当满足上面的条件时,这条规则才会执行,并将域名后面的移到www.123.com/后面
</IfModule>
</VirtualHost>
^/(.*)$表示除域名之外的其他部分,$1也表示这一部分。如https://blog.csdn.net/aoli_shuai/article/list/3,这个^/(.*)$表示/aoli_shuai/article/list/3
域名跳转需要mod_rewrite模块支持
打开虚拟主机配置文件:
[root@shuai-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.111.com www.example.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} !^111.com$
RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L]
</IfModule>
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" common
</VirtualHost>
检测配置文件语法并重新加载配置文件
[root@shuai-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@shuai-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
检测Apache是否加载rewrite模块
[root@shuai-01 ~]# /usr/local/apache2.4/bin/apachectl -M |grep rewrite
没有加载,去配置文件设置
[root@shuai-01 ~]# vi /usr/local/apache2.4/conf/httpd.conf
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule php5_module modules/libphp5.so
[root@shuai-01 ~]# /usr/local/apache2.4/bin/apachectl -M |grep rewrite
rewrite_module (shared)
访问www.example.com会跳转到111.com
[root@shuai-01 ~]# curl -x127.0.0.1:80 www.example.com -I
HTTP/1.1 301 Moved Permanently
Date: Wed, 20 Dec 2017 08:57:28 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Location: http://111.com/
Content-Type: text/html; charset=iso-8859-1