需求,把www.111.com域名跳转到111.com,配置如下:


    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.111.com
     //需要mod_rewrite模块支持
        RewriteEngine on  //打开rewrite功能
        RewriteCond %{HTTP_HOST} !^111.com$  //定义rewrite的条件,主机名(域名)不是www.123.com满足条件
        RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L]   //定义rewrite规则,当满足上面的条件时,这条规则才会执行。301是永久重定向,302是临时重定向
        
         ErrorLog "logs/111.com-error_log"
         CustomLog "logs/111.com-access_log" common

/usr/local/apache2.4/bin/apachectl –t //测试语法
/usr/local/apache2.4/bin/apachectl graceful //重新加载配置文件,不会重启服务

/usr/local/apache2.4/bin/apachectl -M|grep -i rewrite //检测有没有rewrite模块,若无该模块,需要编辑配置文件/usr/local/apache2.4/conf/httpd.conf,删除rewrite_module (shared) 前面的#
curl -x127.0.0.1:80 -I www.111.com //状态码为301(301是永久跳转的意思)
curl -x127.0.0.1:80 -I www.111.com -I //不显示访问的内容只显示状态码