1.8.3 域名跳转(永久)

爱⌒轻易说出口 提交于 2020-03-02 18:45:09

1.8.3 域名跳转

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

<VirtualHost *:80>  
    DocumentRoot "/data/wwwroot/www.111.com"  
    ServerName www.111.com  
    ServerAlias 111.com  
    <IfModule mod_rewrite.c> //需要mod_rewrite模块支持  
        RewriteEngine on  //打开rewrite功能  
        RewriteCond %{HTTP_HOST} !^www.111.com$  //定义rewrite的条件,主机名(域名)不是www.111.com满足条件  
        RewriteRule ^/(.*)$ http://www.111.com/$1 [R=301,L] //定义rewrite规则,当满足上面的条件时,这条规则才会执行  
    </IfModule>  
</VirtualHost> 

/usr/local/apache2/bin/apachectl -M|grep -i rewrite //若无该模块,需要编辑配置文件httpd.conf,删除rewrite_module (shared) 前面的#
curl -x127.0.0.1:80 -I 111.com //状态码为301

编辑配置文件:

[root@Dasoncheng ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
#   ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/data/wwwroot/111.com"
    ServerName www.111.com
    ServerAlias 111.com
#    <Directory /data/wwwroot/111.com>
#    <FilesMatch admin.php>
#        AllowOverride AuthConfig 
#        AuthName "Please pass the auth for this page" 
#        AuthType Basic 
#        AuthUserFile /data/.htpasswd 
#        require valid-user
#    </FilesMatch> 
#    </Directory>
    <IfModule mod_rewrite.c>
       RewriteEngine on
       RewriteCond %{HTTP_HOST} !^www.111.com$
       RewriteRule ^/(.*)$ http://www.111.com/$1 [R=301,L]
    </IfModule>
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common
</VirtualHost>
[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl -M |grep -i rewrite
[root@Dasoncheng ~]# vim /usr/local/apache2.4/conf/httpd.conf
[root@Dasoncheng ~]# grep -i rewrite /usr/local/apache2.4/conf/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so
[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl -M |grep -i rewrite
 rewrite_module (shared)
[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl graceful

测试访问跳转:

[root@Dasoncheng ~]# curl -x 192.168.60.11:80 111.com -I
HTTP/1.1 301 Moved Permanently    ##遇到的第二个提示符(也是httpd.conf里面配置的)301,永久跳转;
Date: Fri, 23 Feb 2018 08:04:27 GMT
Server: Apache/2.4.27 (Unix) PHP/5.6.30
Location: http://www.111.com/
Content-Type: text/html; charset=iso-8859-1


1.8.4 定义访问日志:

访问日志记录用户的每一个请求!
vim /usr/local/apache2.4/conf/httpd.conf //搜索LogFormat
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%h %l %u %t "%r" %>s %b" common
把虚拟主机配置文件改成如下:
<VirtualHost *:80>
 DocumentRoot "/data/wwwroot/www.111.com"
 ServerName www.111.com
 ServerAlias 111.com
 CustomLog "logs/111.com-access_log" combined
</VirtualHost>
重新加载配置文件 -t,graceful
curl -x127.0.0.1:80 -I 111.com
tail /usr/local/apache2.4/logs/111.com-access_log

编辑配置文件:

[root@Dasoncheng ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
#   ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/data/wwwroot/111.com"
    ServerName www.111.com
    ServerAlias 111.com
    <IfModule mod_rewrite.c>
       RewriteEngine on
       RewriteCond %{HTTP_HOST} !^www.111.com$
       RewriteRule ^/(.*)$ http://www.111.com/$1 [R=301,L]
    </IfModule>
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" combined
</VirtualHost>
[root@Dasoncheng ~]# grep "LogFormat" /usr/local/apache2.4/conf/httpd.conf
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl graceful

测试访问日志:

使用三种方式访问:

  1. curl访问
  2. 浏览器访问
  3. referer其他页面跳转
[root@Dasoncheng ~]# curl www.111.com
111.com
[root@Dasoncheng ~]# tail -f /usr/local/apache2.4/logs/111.com-access_log 
192.168.60.1 - - [23/Feb/2018:15:53:50 +0800] "GET / HTTP/1.1" 200 8
192.168.60.1 - - [23/Feb/2018:15:53:50 +0800] "GET / HTTP/1.1" 200 8
192.168.60.1 - - [23/Feb/2018:15:58:21 +0800] "GET / HTTP/1.1" 301 227
192.168.60.1 - - [23/Feb/2018:15:58:21 +0800] "GET / HTTP/1.1" 200 8
192.168.60.1 - - [23/Feb/2018:15:58:48 +0800] "GET / HTTP/1.1" 301 227
192.168.60.1 - zhangsan [23/Feb/2018:15:58:48 +0800] "GET / HTTP/1.1" 200 8
192.168.60.11 - - [23/Feb/2018:16:04:22 +0800] "GET HTTP://111.com/ HTTP/1.1" 301 227
192.168.60.11 - - [23/Feb/2018:16:04:27 +0800] "HEAD HTTP://111.com/ HTTP/1.1" 301 -
##从下面开始,是我用三种方式访问的:
192.168.60.11 - - [23/Feb/2018:16:23:54 +0800] "GET / HTTP/1.1" 200 8 "-" "curl/7.29.0"
192.168.60.1 - zhangsan [23/Feb/2018:16:24:36 +0800] "GET / HTTP/1.1" 200 8 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"
192.168.60.1 - - [23/Feb/2018:16:26:51 +0800] "GET / HTTP/1.1" 200 8 "http://ask.apelearn.com/question/16941" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Tablet PC 2.0; Zoom 3.6.0)"

apache虚拟主机开启php的短标签 http://www.aminglinux.com/bbs/thread-5370-1-1.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!