apache rewrite 规则示例

 1 RewriteRule ^itnews/([0-9]{4})/([0-9]{2})([0-9]{2})/([^/_]*)_([0-9]+).html$ http://www.test.com/news/$1$2/13_$5.html [R=301,C]
 2 
 3 RewriteCond %{REQUEST_FILENAME} ^http://www.test.com(.*)$ 
 4 RewriteCond %{DOCUMENT_ROOT}%1 !-f 
 5 RewriteRule ^http://www.test.com/news/([0-9]{6})/13_([0-9]+).html$ http://www.test.com/news/$1/33_$2.html [R=301,C]
 6 
 7 RewriteCond %{REQUEST_FILENAME} ^http://www.test.com(.*)$ 
 8 RewriteCond %{DOCUMENT_ROOT}%1 !-f  
 9 RewriteRule ^http://www.test.com/news/([0-9]{6})/33_([0-9]+).html$ http://www.test.com/news/$1/11_$2.html [R=301,C]
10 
11 RewriteCond %{REQUEST_FILENAME} ^http://www.test.com(.*)$ 
12 RewriteCond %{DOCUMENT_ROOT}%1 !-f 
13 RewriteRule ^http://www.test.com/news/([0-9]{6})/11_([0-9]+).html$ http://www.test.com/news/$1/12_$2.html [R=301,L]

实现功能把 http://www.test.com/itnews/2013/0101/phone_12354.html 跳转到http://www.test.com/news/201301/(33|11|12)_12354.html

一些问题:

1.flags [chain|C]跳转后,下一个RewriteRule需写绝对地址

2.flags [chain|C]跳转后,RewriteCond 中{REQUEST_FILENAME} 为 http://www.test.com/a/a.html,非/var/www/test/a/a.html

3.  RewriteCond %{REQUEST_FILENAME} ^http://www.test.com(.*)$ [OR]  RewriteCond %{DOCUMENT_ROOT}%1 !-f 错误,RewriteCond语句之间默认的是AND,如果想要OR,则要明确的写出来。

 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^news/([0-9]{6})/12_([0-9]+).html$  http://www.test.com/news/$1/13_$2.html [R=301,C]
RewriteCond %{REQUEST_FILENAME} ^http://www.test.com(.*)$ 
RewriteCond %{DOCUMENT_ROOT}%1 !-f
RewriteRule ^http://www.test.com/news/([0-9]{6})/13_([0-9]+).html$  http://www.test.com/news/$1/33_$2.html [R=301,L]

 3.

RewriteEngine on
Rewriterule ^content-([0-9]+)-([0-9]+)-([0-9]+).html index.php?m=content&c=index&a=show&catid=$1&id=$2&page=$3

RewriteEngine on
Rewriterule ^content-([0-9]+)-([0-9]+)-([0-9]+).html  http://www.xxxx.com/index.php?m=content&c=index&a=show&catid=$1&id=$2&page=$3

由于apache配置环境或者相对路径不同。

4.

if(RewriteCond %{REQUEST_FILENAME} !-f [NC]){
 Apache服务器日志里查询到许多类似这样的警告:
[warn] 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? [R=301,L]

 

 

 

参考网站:

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地址不存在
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”。
(?<=pattern)
反向肯定预查,与正向肯定预查类似,只是方向相反。例如,“(?<=95|98|NT|2000)Windows”能匹配“2000Windows”中的“Windows”,但不能匹配“3.1Windows”中的“Windows”。
(?<!pattern)
反向否定预查,与正向否定预查类似,只是方向相反。例如“(?<!95|98|NT|2000)Windows”能匹配“3.1Windows”中的“Windows”,但不能匹配“2000Windows”中的“Windows”。

RewriteEngine on
RewriteRule ^test/$ /test.php

#RewriteRule ^test/$ http://127.0.0.1/test.php 跳转新页面

RewriteRule ^([a-z]{1}.+)$ 无法达到预期效果

 

 示例:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^fid-([0-9]+).html$
RewriteRule ^thread.php$  http://www.cnblogs.com/thread-%1? [R=301,L]
RewriteCond %{QUERY_STRING} ^t([0-9]+).html$
RewriteRule ^simple/$  http://www.cnblogs.com/read-%1? [R=301,L]
RewriteRule ^simple/$  http://www.cnblogs.com/index.php [R=301,L]

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !.(js|ico|gif|jpe?g|bmp|png|css)$ /index.php [NC,L]

RewriteCond %{QUERY_STRING} ^tid-([0-9]+).html$
RewriteRule ^read.php$  http://www.cnblogs.com/read-%1? [R=301,L]
RewriteCond %{QUERY_STRING} ^tid-([0-9]+)-page-([0-9]+).html$
RewriteRule ^read.php$  http://www.cnblogs.com/read-%1-%2? [R=301,L]

#RewriteRule ^read.php?tid-([0-9]+).html$    http://www.cnblogs.com/read-$1 [R=301,L]
#RewriteRule ^read.php?tid-([0-9]+)-page-([0-9]+).html$    http://www.cnblogs.com/read-$1-$2 [R=301,L]

 

 

bbb目录 rewrite(安装在127.0.0.1/bbb目录)
RewriteEngine on
#RewriteBase /bbb
RewriteCond %{QUERY_STRING} ^(?!index\.php)$
RewriteCond %{REQUEST_URI} !^/bbb/(static|uploads|ueditor|tmp|language|cache|app)
RewriteRule ^(.*)$  /bbb/?/$1 [L]

根目录
RewriteEngine on
RewriteRule ^ueditor/(.*)$  /bbb/ueditor/$1 

 

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

 

posted @ 2013-05-17 10:22  goldenstones  阅读(577)  评论(0编辑  收藏  举报