①编辑虚拟主机配置文件/usr/local/apache2.4/conf/extra/httpd-vhosts.conf如下
<VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com 2111.com.cn ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^111.com$ RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] </IfModule> </VirtualHost>
说明:
<IfModule mod_rewrite.c> //需要mod_rewrite模块支持
RewriteEngine on //打开rewrite功能
RewriteCond %{HTTP_HOST} !^111.com$ //定义rewrite的条件,主机名(域名)111.com满足条件
RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] //定义rewrite规则,当满足上面的条件时,这条规则才会执行。301表示永久重定向的状态码,L表示跳转一次。
②编辑/usr/local/apache2.4/conf/httpd.conf
将LoadModule rewrite_module modules/mod_rewrite.so取消注释
③重启服务
④测试
[[email protected]_01 ~]# curl -x192.168.231.128:80 2111.com.cn -I HTTP/1.1 301 Moved Permanently Date: Thu, 21 Dec 2017 14:05:24 GMT Server: Apache/2.4.28 (Unix) PHP/5.6.30 Location: http://111.com/ Content-Type: text/html; charset=iso-8859-1
时间: 2024-11-09 03:49:23