<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规则,当满足上面的条件时,这条规则才会执行
</IfModule>
</VirtualHost>
<IfModule mod_rewrite.c> //需要mod_rewrite模块支持
RewriteEngine on //打开rewrite功能,on就是打开
RewriteCond %{HTTP_HOST} !^111.com$ //定义rewrite的条件,主机名(域名)不是111.com满足条件。
RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] //定义rewrite规则,当满足上面的条件时,这条规则才会执行
</IfModule>
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
更改前
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/abc.com"
ServerName abc.com
ServerAlias www.abc.com www.123.com
ErrorLog "logs/abc.com-error_log"
CustomLog "logs/abc.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias 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>
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" common
</VirtualHost>
更改后
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/abc.com"
ServerName abc.com
ServerAlias www.abc.com www.123.com
ErrorLog "logs/abc.com-error_log"
CustomLog "logs/abc.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias 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@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@hf-01 ~]#
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -M |grep rewrite
[root@hf-01 ~]#
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf
搜索/rewrite ,找到这一行,并将前面的# 号去掉
LoadModule rewrite_module modules/mod_rewrite.so
然后保存退出
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@hf-01 ~]#
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -M |grep rewrite
rewrite_module (shared)
[root@hf-01 ~]#
[root@hf-01 ~]# cat /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
[root@hf-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1525/master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1199/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1525/master
tcp6 0 0 :::3306 :::* LISTEN 1513/mysqld
tcp6 0 0 :::80 :::* LISTEN 2586/httpd
tcp6 0 0 :::22 :::* LISTEN 1199/sshd
[root@hf-01 ~]#
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@hf-01 ~]# curl -x192.168.74.150:80 2111.com.cn -I
HTTP/1.1 301 Moved Permanently
Date: Wed, 20 Dec 2017 23:32:32 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Location: http://111.com/
Content-Type: text/html; charset=iso-8859-1
[root@hf-01 ~]#
[root@hf-01 ~]# curl -x192.168.74.150:80 2111.com.cn
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://111.com/">here</a>.</p>
</body></html>
[root@hf-01 ~]#
[root@hf-01 ~]# curl -x192.168.74.150:80 2111.com.cn/saasfagssd -I
HTTP/1.1 301 Moved Permanently
Date: Wed, 20 Dec 2017 23:39:50 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Location: http://111.com/saasfagssd
Content-Type: text/html; charset=iso-8859-1
[root@hf-01 ~]#
[root@hf-01 ~]# curl -x192.168.74.150:80 http://111.com/saasfagssd -I
HTTP/1.1 404 Not Found
Date: Wed, 20 Dec 2017 23:42:08 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Content-Type: text/html; charset=iso-8859-1
[root@hf-01 ~]#