Apache域名跳转

域名重定向,产生新域名(新网站)和业务扩展之后,使老客户访问老域名时(或者到期的域名),跳转到新的域名上。ps:为了维护老用户,不会流失用户。

把123.com域名跳转到www.123.com

1.修改httpd-vhosts.conf

cd /usr/local/apache2.4/conf/extra/
vim httpd-vhosts.conf  //增加IfModule
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/usr/local/apache2.4/docs/111.com"
    ServerName www.111.com
    ServerAlias 111.com
    <IfModule mod_rewrite.c>
        RewriteEngine on  
        RewriteCond %{HTTP_HOST} !^111.com$
        RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L]
    </IfModule>
    ErrorLog "logs/abc.com-error_log"
    CustomLog "logs/abc.com-access_log" common
</VirtualHost>

2.创建网站内容

[root@antong extra]# mkdir -p /usr/local/apache2.4/docs/www.111.com
[root@antong extra]# cd usr/local/apache2.4/docs/www.111.com
[root@antong extra]# cat > index.html <EOF
<www.111.com
<EOF
[root@antong extra]# cat > 123.php <EOF
<<?php
<echo "www.111.com/123.php";
<?>

3.加入rewirte模块

vim /usr/local/apache2.4/conf/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so  //将rewrite模块启用
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
[root@antong extra]# /usr/local/apache2.4/bin/apachectl -M | grep rewrite
 rewrite_module (shared)

4.进行域名跳转测试

[root@antong extra]# curl -xlocalhost:80 2111.com.cn -I  //-I参数用来不显示网页内容,只返回代码
HTTP/1.1 301 Moved Permanently
Date: Wed, 04 Aug 2021 17:10:33 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.30
Location: http://111.com/
Content-Type: text/html; charset=iso-8859-1

[root@antong extra]# curl -xlocalhost:80 3.com.cn -I    
HTTP/1.1 301 Moved Permanently
Date: Wed, 04 Aug 2021 17:13:02 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.30
Location: http://111.com/
Content-Type: text/html; charset=iso-8859-1

[root@antong extra]# curl -xlocalhost:80 3.com.cn/admin.php -I
HTTP/1.1 301 Moved Permanently
Date: Wed, 04 Aug 2021 17:14:40 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.30
Location: http://111.com/admin.php
Content-Type: text/html; charset=iso-8859-1

本随笔中配置的域名跳转,只要域名不是111.com开头结尾都会跳转到www.111.com,子内容也会跳转到www.111.com的子内容下。