【CentOS 7架構8】,域名跳轉#
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 ^(.
-
/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若跳轉成功,則域