1. 程式人生 > >LAMP-域名跳轉

LAMP-域名跳轉

apache

當公司的網站更新了新的站點域名,希望原有的域名依舊能夠提供服務,這時就需要使用域名跳轉。用戶訪問原有域名時,自動跳轉到新的域名,訪問新域名所對應的網站頁面。


1、修改虛擬主機配置

[[email protected] ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 
<VirtualHost *:80>
    DocumentRoot "/data/www/abc.com"
    ServerName abc.com
    <IfModule mod_rewrite.c>         ##需要mod_rewrite模塊支持
        RewriteEngine on             ##打開rewrite功能
        RewriteCond %{HTTP_HOST} !^abc.com$                    ##定義rewrite的條件
        RewriteRule ^(/.*)$ http://www.baidu.com/$1 [R=301,L]  ##定義rewrite規則
    </IfModule>
</VirtualHost>


2、檢查重新加載

[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -M | grep -i rewrite
[[email protected] ~]# vi /usr/local/apache2.4/conf/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so           ##解除註釋啟用
[[email protected]
/* */ ~]# /usr/local/apache2.4/bin/apachectl -M | grep -i rewrite rewrite_module (shared)


3、驗證測試

[[email protected] ~]# curl -x127.0.0.1:80 abc.com -I    ##-I:不顯示站內內容,只顯示狀態碼
HTTP/1.1 200 OK
Date: Fri, 21 Jul 2017 09:34:55 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Content-Type: text/html; charset=UTF-8

[[email protected]
/* */ ~]# curl -x127.0.0.1:80 abcd.com -I HTTP/1.1 301 Moved Permanently ##301 跳轉 Date: Fri, 21 Jul 2017 09:35:01 GMT Server: Apache/2.4.27 (Unix) PHP/7.1.6 Location: http://abc.com/ Content-Type: text/html; charset=iso-8859-1 [[email protected] ~]# curl -x127.0.0.1:80 abcd.com/123.php -I HTTP/1.1 301 Moved Permanently Date: Fri, 21 Jul 2017 09:39:00 GMT Server: Apache/2.4.27 (Unix) PHP/7.1.6 Location: http://abc.com/123.php Content-Type: text/html; charset=iso-8859-1


本文出自 “Gorilla Grodd” 博客,請務必保留此出處http://juispan.blog.51cto.com/943137/1952800

LAMP-域名跳轉