shallow丿ove
域名跳转
-
需求,将123.com域名跳转到www.123.com,配置如下: <VirtulHost :80> DocumentRoot "/data/wwwroot/www.123.com ServerName www.123.com ServerAlias 123.com <IfModule mod_rewrite.c> #需要mod_rewrite模块支持 RewriteEngine on #打开rewrite功能 RewriteCond %{HTTP_HOST}!^111.com$ #定义rewrite的条件,主机名(域名)不是111.com满足条件,$表示结尾,不然只要包括111.com也是满足条件 RewriteRule ^(.)$http://www.123.com/$1 [R=301,L] #定义rewrite规则,当满足上面的条件时,这条规则才会执行 </IfModule> </VirtulHost>
-
/usr/local/apache2.4/bin/apachectl -M | grep -i rewrite #若无该模块,需要编辑配置文件httpd.conf,删除rewrite_module(shared)前面的#
-
curl -x 127.0.0.1:80 -I 123.com #状态码为301
-
权重
-
seo搜索引擎优化
-
301重定向
主要是利用搜索引擎的方法将网站原有的权重作域名跳转到另一个网站中,使其权重不变
[root@localhost ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
31 <VirtualHost *:80>
32 DocumentRoot "/data/wwwroot/111.com"
33 ServerName 111.com
34 ServerAlias www.example.com
35 # <Directory /data/wwwroot/111.com>
36 # <FilesMatch user.php>
37 # AllowOverride AuthConfig
38 # AuthName "111.com user auth"
39 # AuthType Basic
40 # AuthUserFile /data/.htpasswd
41 # require valid-user
42 # </FilesMatch>
43 # </Directory>
44 <IfModule mod_rewrite.c>
45 RewriteEngine on
46 RewriteCond %{HTTP_HOST} !^www.123.com$
47 RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L]
48 </IfModule>
49 ErrorLog "logs/111.com-error_log"
50 CustomLog "logs/111.com-access_log" common
51 </VirtualHost>
,^/(.*)$指域名后面的部分,$1指前面第一个括号,若有指定则添加/$2/$3 永久重定则要定义301状态码,L表示指跳一次 302表示临时重定向,但搜索引擎则不会把原来的重定向降低
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
检查模块
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -M | grep rewrite
[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf
144 #LoadModule vhost_alias_module modules/mod_vhost_alias.so
145 #LoadModule negotiation_module modules/mod_negotiation.so
146 LoadModule dir_module modules/mod_dir.so
147 #LoadModule actions_module modules/mod_actions.so
148 #LoadModule speling_module modules/mod_speling.so
149 #LoadModule userdir_module modules/mod_userdir.so
150 LoadModule alias_module modules/mod_alias.so
151 #LoadModule rewrite_module modules/mod_rewrite.so
152 LoadModule php5_module modules/libphp5.so
153 #LoadModule php7_module modules/libphp7.so
154
155 <IfModule unixd_module>
156 #
157 # If you wish httpd to run as a different user or group, you must run
158 # httpd as root initially and it will switch.
159 #
将151行LoadModule rewrite_module modules/mod_rewrite.so的注释去掉
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -M | grep rewrite
rewrite_module (shared)
[root@localhost ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 3013/master
tcp 0 0 0.0.0.0:39162 0.0.0.0:* LISTEN 1533/rpc.statd
tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:39879 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:20048 0.0.0.0:* LISTEN 1922/rpc.mountd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1401/sshd
tcp6 0 0 ::1:25 :::* LISTEN 3013/master
tcp6 0 0 :::2049 :::* LISTEN -
tcp6 0 0 :::54754 :::* LISTEN -
tcp6 0 0 :::56495 :::* LISTEN 1533/rpc.statd
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::80 :::* LISTEN 24752/httpd
tcp6 0 0 :::20048 :::* LISTEN 1922/rpc.mountd
tcp6 0 0 :::22 :::* LISTEN 1401/sshd
:::80监听全网使用127.0.0.1或者本机的ip地址都能访问
[root@localhost ~]# curl -x 192.168.9.134:80 www.example.com -I
HTTP/1.1 301 Moved Permanently
Date: Sat, 04 Nov 2017 05:44:37 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Location: http://111.com/
Content-Type: text/html; charset=iso-8859-1
[root@localhost ~]# curl -x 192.168.9.134:80 www.example.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://111.com/">here</a>.</p>
</body></html>
[root@localhost ~]# curl -x 192.168.9.134:80 www.example.com/111111 -I
HTTP/1.1 301 Moved Permanently
Date: Sat, 04 Nov 2017 05:46:24 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Location: http://111.com/111111
Content-Type: text/html; charset=iso-8859-1
[root@localhost ~]# curl -x 192.168.9.134:80 www.example.com/111111
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://111.com/111111">here</a>.</p>
</body></html>
[root@localhost ~]# curl -x 192.168.9.134:80 http://111.com/111111 -I
HTTP/1.1 404 Not Found
Date: Sat, 04 Nov 2017 05:47:48 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Content-Type: text/html; charset=iso-8859-1
[root@localhost ~]# curl -x 192.168.9.134:80 http://111.com/user.php -I
HTTP/1.1 200 OK
Date: Sat, 04 Nov 2017 05:48:28 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
X-Powered-By: PHP/5.6.30
Content-Type: text/html; charset=UTF-8
[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf
197 #
198 # Deny access to the entirety of your server's filesystem. You must
199 # explicitly permit access to web content directories in other
200 # <Directory> blocks below.
201 #
202 <Directory />
203 AllowOverride none
204 # Require all granted
205 Require all denied
206 </Directory>
207
208 #
209 # Note that from this point forward you must specifically allow
210 # particular features to be enabled - so if something's not working as
211 # you might expect, make sure that you have specifically enabled it
212 # below.
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]# curl -x 192.168.9.134:80 111.com -I
HTTP/1.1 403 Forbidden
Date: Sat, 04 Nov 2017 05:55:13 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Content-Type: text/html; charset=iso-8859-1
改回原来的
[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf
197 #
198 # Deny access to the entirety of your server's filesystem. You must
199 # explicitly permit access to web content directories in other
200 # <Directory> blocks below.
201 #
202 <Directory />
203 AllowOverride none
204 Require all granted
205 # Require all denied
206 </Directory>
207
208 #
209 # Note that from this point forward you must specifically allow
210 # particular features to be enabled - so if something's not working as
211 # you might expect, make sure that you have specifically enabled it
212 # below.
然后在Windows下测试,浏览器访问www.example.com若跳转成功,则域名跳转完成