rewrite使用

在httpd.conf中添加:

LoadModule rewrite_module modules/mod_rewrite.so

<IfModule mod_rewrite.c>

RewriteEngine On 

RewriteLog     logs/rewrite.log #添加转发日志

RewriteLogLevel 3  #添加转发日志

RewriteRule ^/abc/([a-z]+)/([0-9]+).html$ /test.jsp?user=$1&password=$2 [R,L]

</IfModule>

 

启动apache后,访问

http://localhost/abc/asdfdg/13653.html 即会重定向到

http://localhost/test.jsp?user=asdfdg&password=13653

 

需要说明的是:

RewriteRule ^/abc/([a-z]+)/([0-9]+).html$ /test.jsp?user=$1&password=$2 [R,L]

则重定向后,显示新地址;

RewriteRule ^/abc/([a-z]+)/([0-9]+).html$ /test.jsp?user=$1&password=$2 [PT]

显示的依然是旧地址;

如果为RewriteRule ^/abc/([a-z]+)/([0-9]+).html$ /test.jsp?user=$1&password=$2 或者

RewriteRule ^/abc/([a-z]+)/([0-9]+).html$ /test.jsp?user=$1&password=$2 [L]则第一次请求显示test.jsp页面但是参数为传递进去,再次刷新下则显示test.jsp的源代码,这种现象应该是跟rule 规则有关。。。。以后要用时在仔细研究下。。。

 

以下是规则:

Apache mod_rewrite规则重写的标志一览 

1) R[=code](force redirect) 强制外部重定向 
强制在替代字符串加上http://thishost[:thisport]/前缀重定向到外部的URL.如果code不指定,将用缺省的302 HTTP状态码。 
2) F(force URL to be forbidden)禁用URL,返回403HTTP状态码。 
3) G(force URL to be gone) 强制URL为GONE,返回410HTTP状态码。 
4) P(force proxy) 强制使用代理转发。 
5) L(last rule) 表明当前规则是最后一条规则,停止分析以后规则的重写。 
6) N(next round) 重新从第一条规则开始运行重写过程。 
7) C(chained with next rule) 与下一条规则关联 

如果规则匹配则正常处理,该标志无效,如果不匹配,那么下面所有关联的规则都跳过。 

8) T=MIME-type(force MIME type) 强制MIME类型 
9) NS (used only if no internal sub-request) 只用于不是内部子请求 
10) NC(no case) 不区分大小写 
11) QSA(query string append) 追加请求字符串 
12) NE(no URI escaping of output) 不在输出转义特殊字符 
例如:RewriteRule /foo/(.*) /bar?arg=P1%3d$1 [R,NE] 将能正确的将/foo/zoo转换成/bar?arg=P1=zoo 
13) PT(pass through to next handler) 传递给下一个处理 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值