Apache的rewrite的设置参数

Apache的rewrite的重写非常常用,现总结了一下.

Apache mod_rewrite规则重写的标志一览

R[=code](force redirect) 强制外部重定向
强制在替代字符串加上http://thishost[:thisport]/前缀重定向到外部的URL.如果code不指定,将用缺省的302 HTTP状态码。
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
PT(pass through to next handler) 传递给下一个处理
例如:
RewriteRule ^/abc(.*) /def$1 [PT] # 将会交给/def规则处理
Alias /def /ghi
S=num(skip next rule(s)) 跳过num条规则
E=VAR:VAL(set environment variable) 设置环境变量

使用mod_rewrite时常用的服务器变量:
HTTP headers:HTTP_USER_AGENT, HTTP_REFERER, HTTP_COOKIE, HTTP_HOST, HTTP_ACCEPT
connection & request: REMOTE_ADDR, QUERY_STRING
server internals: DOCUMENT_ROOT, SERVER_PORT, SERVER_PROTOCOL
system stuff: TIME_YEAR, TIME_MON, TIME_DAY

RewriteRule规则表达式的说明:
. 匹配任何单字符
[chars] 匹配字符串:chars
[^chars] 不匹配字符串:chars
text1|text2 可选择的字符串:text1或text2
? 匹配0到1个字符
* 匹配0到多个字符
+ 匹配1到多个字符
^ 字符串开始标志
$ 字符串结束标志
\n 转义符标志

反向引用 $N 用于 RewriteRule 中匹配的变量调用(0 <= N <= 9)
反向引用 %N 用于 RewriteCond 中最后一个匹配的变量调用(1 <= N <= 9)

RewriteCond适用的标志符
‘nocase|NC’ (no case)忽略大小
‘ornext|OR’ (or next condition)逻辑或,可以同时匹配多个RewriteCond条件

RewriteRule适用的标志符
‘redirect|R [=code]’ (force redirect)强迫重写为基于http开头的外部转向(注意URL的变化) 如:[R=301,L]
‘forbidden|F’ (force URL to be forbidden)重写为禁止访问
‘proxy|P’ (force proxy)重写为通过代理访问的http路径
‘last|L’ (last rule)最后的重写规则标志,如果匹配,不再执行以后的规则
‘next|N’ (next round)循环同一个规则,直到不能满足匹配
‘chain|C’ (chained with next rule)如果匹配该规则,则继续下面的有Chain标志的规则。
‘type|T=MIME-type’ (force MIME type)指定MIME类型
‘nosubreq|NS’ (used only if no internal sub-request)如果是内部子请求则跳过
‘nocase|NC’ (no case)忽略大小
‘qsappend|QSA’ (query string append)附加查询字符串
‘noescape|NE’ (no URI escaping of output)禁止URL中的字符自动转义成%[0-9]+的形式。
‘passthrough|PT’ (pass through to next handler)将重写结果运用于mod_alias
’skip|S=num’ (skip next rule(s))跳过下面几个规则
‘env|E=VAR:VAL’ (set environment variable)添加环境变量

实战
例子:
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^MSIE [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Opera [NC]
RewriteRule ^.* – [F,L] 这里”-”表示没有替换,浏览器为IE和Opera的访客将被禁止访问。

例子:
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ([^/]+)$ /test/$1.php
#for example: /test/admin => /test/admin.php
RewriteRule ([^/]+)\.html$ /test/$1.php [L]
#for example: /test/admin.html => /test/admin.php

限制目录只能显示图片

<pre>
< IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^.*\.(gif|jpg|jpeg|png|swf)$
RewriteRule .*$ – [F,L]
< /IfModule>

</pre>

将域名重定向到 www.domainname.com

<pre>

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domainname\.com$ [NC]
RewriteRule ^(.*)$ http://www.domainname.com/$1 [R=301,L]

</pre>

491 thoughts on “Apache的rewrite的设置参数

  1. Pingback: Google

  2. Rico Gueits

    Vouêtes vraiment bien informé et très intelligent. Vous avez écrit quelque chose que les gens puissent comprendre et faire l’objet fascinant pour tout le monde. Je suis sauver ce pour une utilisation future.

  3. Chang Blackbird

    I simply wanted to thank you so much once more. I’m not certain the things I could possibly have created in the absence of the actual smart ideas revealed by you on my situation. It actually was a alarming difficulty for me, however , looking at a professional tactic you dealt with that forced me to cry with gladness. Extremely happy for your service and even wish you really know what an amazing job you were accomplishing training other individuals all through a site. More than likely you haven’t come across any of us.

  4. Ora Hemming

    Hey There. I found your blog using msn. This is an extremely well written article. I’ll be sure to bookmark it and return to read more of your useful info. Thanks for the post. I’ll definitely comeback.

  5. Kristofer Calderara

    There are very lots of details that adheres to that to think about. That’s a fantastic specify retrieve. I provide you with the thoughts above as general inspiration but clearly you will find questions such as the one you raise up where biggest factor are going to be getting work done in honest great faith. I don?t know if guidelines have emerged around such things as that, but More than likely that the job is clearly defined as an affordable game. Both youngsters feel the impact of merely a moment’s pleasure, throughout their lives.

  6. Albert Bezdicek

    What your stating is absolutely genuine. I know that everyone ought to say the identical factor, but I just feel that you set it in a way that absolutely everyone can realize. I also adore the photographs you set in here. They fit so nicely with what youre hoping to say. Im guaranteed youll attain so numerous people today with what youve got to say.

  7. Gemma Edmister

    It was something of great contentment getting to your site recently. I arrived here this day hoping to find out something new. I was not disappointed. Your ideas on new strategies on this area were enlightening and a fantastic help to me personally. Thank you for creating time to write down these things as well as for sharing your ideas.

  8. Hedwig Maresh

    cheers for such a brilliant site. Where else could someone get that kind of info written in such a perfect way? I have a presentation that I am presently working on, and I have been on the watch out for such information.

  9. Isaias Kook

    Oh my goodness! a great post dude. Thanks However My business is experiencing problem with ur rss . Do not know why Struggling to enroll in it. Is there everyone acquiring identical rss problem? Anybody who knows kindly respond. Thnkx

  10. Stanford Setaro

    Can I say what relief to locate one who really knows what theyre talking about on the web. You certainly understand how to bring a challenge to light and earn it essential. More and more people need to ought to see this and understand why side of your story. I cant think youre not more common because you certainly have the gift.

  11. Halley Roarty

    After study some of the web sites on your web site now, and i really much like your technique of blogging. I bookmarked it to my bookmark web site list and will also be checking back soon. Pls look into my web site likewise and make me aware what you think.

  12. Deanne Urzua

    Superb work! This really is the type of info that should be shared around the web. Shame on the search engines for not positioning this post higher! Come on over and visit my internet site . Thanks =)

  13. Agustin Sivills

    After study some of the content in your web site now, and i also really much like your technique of blogging. I bookmarked it to my bookmark web site list and will also be checking back soon. Pls take a look at my internet site as well and told me what you believe.

  14. Julian Busher

    This is the appropriate blog for anyone who wants to learn about this topic. You are aware of so much its virtually not easy to argue together with you (not that When i would want…HaHa). You actually put a brand new spin on a topic thats been discussing for some time. Wonderful stuff, just wonderful!

  15. Phillip Tepezano

    I’ve been gone for some time, but now I remember why I used to love this web site. Thank you, I’ll try and check back more frequently. How often do you update your website?

  16. Traci Tackette

    Thank you for another excellent article. Where else may anybody get that kind of info in such an ideal way of writing? I’ve a presentation subsequent week, and I’m on the look for such info.

  17. Del Vidro

    hi!,I love your writing very much! proportion we be in contact more about your post on AOL? I require a specialist on this area to resolve my problem. May be that’s you! Taking a look forward to look you.

  18. Suzie Vigiano

    Thanks a lot for sharing this with all people you really recognise what you’re talking approximately! Bookmarked. Please also seek advice from my website =). We may have a hyperlink exchange contract among us!

  19. Lean Thoby

    I’m impressed, I must say. Genuinely rarely must i encounter a blog that’s both educative and entertaining, and without a doubt, you’ve got hit the nail about the head. Your notion is outstanding; the issue is an issue that too few persons are speaking intelligently about. I will be very happy that we found this inside my hunt for something concerning this.

  20. Jeffery Nierman

    Terrific work! That is the kind of information that are meant to be shared across the web. Disgrace on the seek for now not positioning this submit upper! Come on over and seek advice from my website . Thank you =)

  21. Zackary Zimerman

    Aw, this is an extremely good post. In thought I must set up writing such as this moreover – spending time and actual effort to create a great article… but what things can I say… I procrastinate alot and no means often get something carried out.

  22. Douglas Antonello

    I have to get across my passion for your kind-heartedness in support of persons that really want assistance with this particular niche. Your special dedication to passing the solution all around appeared to be exceedingly beneficial and have all the time enabled some individuals much like me to achieve their goals. Your new invaluable facts entails a lot a person like me and much more to my colleagues. Thank you; from each one of us.

  23. Marcelle Svennungsen

    Very nice post. I just stumbled upon your weblog and wished to say that I have truly enjoyed browsing your blog posts. In any case I will be subscribing to your feed and I hope you write again soon!

发表评论