apache rewrite 规则示例
1 RewriteRule ^itnews/({4})/({2})({2})/([^/_]*)_(+).html$ http://www.test.com/news/$1$2/13_$5.html2
3 RewriteCond %{REQUEST_FILENAME} ^http://www.test.com(.*)$
4 RewriteCond %{DOCUMENT_ROOT}%1 !-f
5 RewriteRule ^http://www.test.com/news/({6})/13_(+).html$ http://www.test.com/news/$1/33_$2.html
6
7 RewriteCond %{REQUEST_FILENAME} ^http://www.test.com(.*)$
8 RewriteCond %{DOCUMENT_ROOT}%1 !-f
9 RewriteRule ^http://www.test.com/news/({6})/33_(+).html$ http://www.test.com/news/$1/11_$2.html
10
11 RewriteCond %{REQUEST_FILENAME} ^http://www.test.com(.*)$
12 RewriteCond %{DOCUMENT_ROOT}%1 !-f
13 RewriteRule ^http://www.test.com/news/({6})/11_(+).html$ http://www.test.com/news/$1/12_$2.html
实现功能把 http://www.test.com/itnews/2013/0101/phone_12354.html 跳转到http://www.test.com/news/201301/(33|11|12)_12354.html
一些问题:
1.flags 跳转后,下一个RewriteRule需写绝对地址
2.flags 跳转后,RewriteCond 中{REQUEST_FILENAME} 为 http://www.test.com/a/a.html,非/var/www/test/a/a.html
3.RewriteCond %{REQUEST_FILENAME} ^http://www.test.com(.*)$ RewriteCond %{DOCUMENT_ROOT}%1 !-f 错误,RewriteCond语句之间默认的是AND,如果想要OR,则要明确的写出来。
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^news/({6})/12_(+).html$http://www.test.com/news/$1/13_$2.html
RewriteCond %{REQUEST_FILENAME} ^http://www.test.com(.*)$
RewriteCond %{DOCUMENT_ROOT}%1 !-f
RewriteRule ^http://www.test.com/news/({6})/13_(+).html$http://www.test.com/news/$1/33_$2.html
3.
RewriteEngine on
Rewriterule ^content-(+)-(+)-(+).html index.php?m=content&c=index&a=show&catid=$1&id=$2&page=$3
RewriteEngine on
Rewriterule ^content-(+)-(+)-(+).htmlhttp://www.xxxx.com/index.php?m=content&c=index&a=show&catid=$1&id=$2&page=$3
由于apache配置环境或者相对路径不同。
4.
if(RewriteCond %{REQUEST_FILENAME} !-f ){
Apache服务器日志里查询到许多类似这样的警告:
RewriteCond: NoCase option for non-regex pattern '-f' is not supported and will be ignored.
}
5.
RewriteCond %{QUERY_STRING} ^a=(.*)$
RewriteRule ^test1.php$http://www.test.com/test%1.html?
参考网站:
http://www.sitepoint.com/apache-mod_rewrite-examples-2/
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
速查表:
RewirteRule 标记
含义
描述
R
Redirect
发出一个HTTP重定向
F
Forbidden
禁止对URL地址的存取
G
Gone
标记URL地址不存在
P
Proxy
将URL地址传递至mod_proxy
L
Last
停止处理接下来的规则
N
Next
再次从第一个规则开始处理,但是使用当前重写后的URL地址
C
Chain
将当前的规则和紧随其后的规则链接起来
T
Type
强制执行指明的MIME类
NS
Nosubreq
只在没有任何内部子请求执行时运用本规则
NC
Nocase
URL地址匹配对大小写敏感(不区分大小写)
QSA
Qsappend
在新的URL地址后附加查询字符串部分,而不是替代
PT
Passthrough
将重写后的URL地址传递给另一个Apache模块进行进一步处理
S
Skip
忽略之后的规则
E
Env
设置环境变量
(?:pattern)
匹配pattern但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用或字符“(|)”来组合一个模式的各个部分是很有用。例如“industr(?:y|ies)”就是一个比“industry|industries”更简略的表达式。
(?=pattern)
正向肯定预查,在任何匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如,“Windows(?=95|98|NT|2000)”能匹配“Windows2000”中的“Windows”,但不能匹配“Windows3.1”中的“Windows”。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。
(?!pattern)
正向否定预查,在任何不匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如“Windows(?!95|98|NT|2000)”能匹配“Windows3.1”中的“Windows”,但不能匹配“Windows2000”中的“Windows”。
(?
页:
[1]