简介

重定向(Redirect)就是通过各种方法将各种网络请求重新定个方向转到其它位置 我们在网站建设中,时常会遇到需要网页重定向的情况: 1.网站调整(如改变网页目录结构); 2.网页被移到一个新地址; 3.网页扩展名改变(如应用需要把.php改成.Html或.shtml)。 这种情况下,如果不做重定向,则用户收藏夹或搜索引擎数据库中旧地址只能让访问客户得到一个404页面错误信息,访问流量白白丧失;再者某些注册了多个域名的网站,也需要通过重定向让访问这些域名的用户自动跳转到主站点等 常用的重定向方式 301 redirect:301代表永久性转移(Permanently Moved),301重定向是网页更改地址后对搜索引擎友好的最好方法,只要不是暂时搬移的情况,都建议使用301来做转址 302 redirect:302代表暂时性转移(Temporarily Moved ),在前些年,不少Black Hat SEO(黑帽SEO)曾广泛应用这项技术作弊,目前,各大主要搜索引擎均加强了打击力度,像Google前些年对域名之王(Business)以及近来对BMW德国网站的惩罚。即使网站客观上不是spam,也很容易被搜寻引擎容易误判为spam而遭到惩罚 meta fresh:这在2000年前比较流行,不过现在已很少见。其具体是通过网页中的meta指令,在特定时间后重定向到新的网页,如果延迟的时间太短(约5秒之内),会被判断为spam

常用参数

结尾标识符

R[=code]:强制外部重定向
G:强制URL为gone,返回状态码410
P:强制进行代理转发
L:当前规则为最后一条匹配规则
N:重新从第一条规则开始匹配
C:与下一条规则关联
T=MIME-TYPE:强制MIME类型
NC:不区分大小写
OR:表示或规则

表达式

.:匹配任何单字符
[string]:匹配字符串string
[^string]:不匹配字符串string
str1|str2:可选择的字符串str1|str2
?:匹配0-1个字符
*:匹配0到多个字符
+:匹配1到多个字符
^:字符串开始符号
$:字符串结束符号
\n:转义符标志

变量

HTTP_USER_AGENT:使用的代理
HTTP_REFERER:通过哪个网页转到网站
HTTP_COOKIE:客户端存储的cookie信息
HTTP_ACCEPT:客户端支持的MIME类型
REMOTE_ADDR:客户端IP地址
QUERY_STRING:URL的访问字符串
DOCUMENT_ROOT:服务器发布目录
SERVER_PORT:服务器端口
SERVER_PROTOCOL:服务端协议
TIME_YEAR:年
TIME_MON:月
TIME:DAY:日

案例

从www.a.com、www.b.com、www.c.com跳转到远端服务器主页上

[root@localhost ~]# vi /etc/httpd/conf/httpd.conf 
RewriteEngine on
RewriteCond %{HTTP_HOST} www.b.com		[NC,OR]
RewriteCond %{HTTP_HOST} www.a.com		[NC,OR]
RewriteCond %{HTTP_HOST} www.c.com		[NC]
RewriteRule ^/$ http://ip/	[L]
#重启服务
[root@localhost ~]# systemctl restart httpd.service 

从www.a.com、www.b.com、www.c.com永久重定向到远端服务器相应页面上

[root@localhost ~]# vi /etc/httpd/conf/httpd.conf 
RewriteEngine on
RewriteCond %{HTTP_HOST} www.b.com		[NC,OR]
RewriteCond %{HTTP_HOST} www.a.com		[NC,OR]
RewriteCond %{HTTP_HOST} www.c.com		[NC]
RewriteRule ^/(.*)$ http://ip/$1	[L,R=301]
#$1表示前方()中的所有内容
#重启服务
[root@localhost ~]# systemctl restart httpd.service 

把www.a.com、www.b.com、www.c.com跳转到远端服务器相应目录的页面上

[root@localhost ~]# vi /etc/httpd/conf/httpd.conf 
RewriteEngine on
RewriteCond %{HTTP_HOST} www.b.com		[NC,OR]
RewriteCond %{HTTP_HOST} www.a.com		[NC,OR]
RewriteCond %{HTTP_HOST} www.c.com		[NC]
RewriteRule ^/$ http://ip/ccc/bbb/2.html	[L]
#重启服务
[root@localhost ~]# systemctl restart httpd.service 

把访问本地/abc/a/1.html的请求跳转到远端服务器的ccc/bbb/2.html中

[root@localhost ~]# vi /etc/httpd/conf/httpd.conf 
RewriteEngine on
RewriteRule ^/abc/a/1.html$ http://ip/ccc/bbb/2.html	[L]
#重启服务
[root@localhost ~]# systemctl restart httpd.service 

把访问本地hello.php?pid=2跳转至远端服务器的/ccc/bbb/2.html中

[root@localhost ~]# vi /etc/httpd/conf/httpd.conf 
RewriteEngine on
RewriteCond %{QUERY_STRING} ^pid=(.+)$		[NC]
RewriteRule ^/hello\.php$ http://ip/ccc/bbb/%1.html	[L]
#%1表示规则()中的内容
#重启服务
[root@localhost ~]# systemctl restart httpd.service 

把访问本地hello.php?pid=1&tid=2跳转至远端服务器的/ccc/bbb/1/2.html中

[root@localhost ~]# vi /etc/httpd/conf/httpd.conf 
RewriteEngine on
RewriteCond %{QUERY_STRING} ^pid=(.+)&tid=(.+)$		[NC]
RewriteRule ^/hello\.php$ http://ip/ccc/bbb/%1/%2.html	[L]
#重启服务
[root@localhost ~]# systemctl restart httpd.service 

若使用移动端访问网站,跳转到远端服务器的首页

[root@localhost ~]# vi /etc/httpd/conf/httpd.conf 
RewriteEngine on
RewriteCond %{HTTP_USER_AGNET} ^iPhone		[NC,OR]
RewriteCond %{HTTP_USER_AGNET} ^Android		[NC]
RewriteRule ^/(.*)$ http://ip/index.html/$1	[L,R=301]
#重启服务
[root@localhost ~]# systemctl restart httpd.service 

HTTPS重定向设置

[root@localhost ~]# vi /etc/httpd/conf/httpd.conf 
RewriteEngine On
RewriteRule ^/(.*)$ https://localhost/$1 
#重启服务
[root@localhost ~]# systemctl restart httpd.service