Apache Rewrite 重定向问题汇总

Apache的RewriteRule规则详细介绍:

R[=code](force redirect) 强制外部重定向

F(force URL to be forbidden)禁用URL,返回403HTTP状态码。

G(force URL to be gone) 强制URL为GONE,返回410HTTP状态码。

P(force proxy) 强制使用代理转发。 )

L(last rule) 表明当前规则是最后一条规则,停止分析以后规则的重写。
N(next round) 重新从第一条规则开始运行重写过程。
C(chained with next rule) 与下一条规则关联
如果规则匹配则正常处理,该标志无效,如果不匹配,那么下面所有关联的规则都跳过。

T=MIME-type(force MIME type) 强制MIME类型
NS (used only if no internal sub-request) 只用于不是内部子请求

NC(no case) 不区分大小写
QSA(query string append) 追加请求字符串

NE(no URI escaping of output) 不在输出转义特殊字符

例如:RewriteRule /foo/(.*) /bar?arg=P1/%3d$1 [R,NE] 将能正确的将/foo/zoo转换成/bar?arg=P1=zed +3a?` Z
PT(pass through to next handler) 传递给下一个处理

例如: :5/Uh/ sX
RewriteRule ^/abc(.*) /def$1 [PT] # 将会交给/def规则处理

S=num(skip next rule(s)) 跳过num条规则

E=VAR:VAL(set environment variable) 设置环境变量

 

实用的实例:

 

简单的虚拟主机配置:

<VirtualHost *:80>
DocumentRoot E:/JAVA_WorkSpace/bsei_mapenjoy_passport_tomcat/WebRoot
ServerName www.boshilian.com
ErrorDocument 404   /index.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule /22.htm(.+)$ /help.jsp?$1 [L]
RewriteRule /33.htm(.+)$ /help.jsp?$1 [R]
RewriteRule /44.htm(.+)$ http://www.sohu.com?$1 [L]
RewriteRule /55.htm(.+)$ http://www.sohu.com?$1 [R]

RewriteRule /66.htm(.+)$ http://www.sohu.com?$1 [P]

RewriteCond %{QUERY_STRING} ^t/=(.+)?$ [NC]
RewriteRule ^/api$ http://www.sina.com.cn?t=%1 [P]

</IfModule>
</VirtualHost>

 

最简单的重定向:

 

下面列一下常用简单的,相信大家都熟悉,我就简单列一下:

 

url不发生变化,直接定向,类似于java中的forword:

RewriteRule /22.htm(.+)$ /help.jsp?$1 [L]

 

url发生变化,直接定向,redirect转向,(支持跨域):
RewriteRule /33.htm(.+)$ /help.jsp?$1 [R]

RewriteRule /44.htm(.+)$ http://www.sohu.com?$1 [L]
RewriteRule /55.htm(.+)$ http://www.sohu.com?$1 [R]

除非注明, 风波竹林 文章均为原创,转载请以链接形式标明本文地址
本文地址: https://blog.fbzl.org/apache-rewrite-重定向问题汇总/

发表评论