1. 程式人生 > >apache域名跳轉

apache域名跳轉

跳轉

①編輯虛擬主機配置文件/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取消註釋

③重啟服務

④測試

[root@test_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


apache域名跳轉