Apache的重写模块,复杂的规则 [英] Apache Rewrite module, complex rules
问题描述
我基本上要重定向所有请求的index.php什么并不重要,除了那些具有一定 REQUEST_URI
。看起来就像图像文件的请求,所以有一个像结尾: .JPG
或巴纽
应进行检查,如果他们是在公共/
文件夹(或任何深度的所有子文件夹),如果他们是他们应该提供服务,并重写的过程应该到此为止了!如果没有,我想重定向到一个默认的形象公共/错误/图像未found.png
并终止重写的过程。其他的例外是与的.js,的CSS,html的
或瑞士法郎
结尾的文件。他们也应该只投放,如果他们设下的公共/
文件夹或任何其他子文件夹。如果不是这样,一个简单的 404-未找到
应发回。在最后一个重写的过程中这两种情况下需要停止课程。
任何其他请求应该被重定向到的index.php
及附加为查询字符串
。 (即使该请求指向一个目录或文件不在的条件下上述,但存在的,例如: www.xyz.com/library/Database.php
- > www.xyz.com/index.php?url=library/Database.php
)
I basically want to redirect all requests to index.php doesn't matter what, except those with certain REQUEST_URI
. Those requests that look like image files, so have an ending like: .jpg
or .png
should be examined and if they are under the public/
folder (or any subfolders in any depth) and if they are they should be served and the rewriting process should stop here! If not, I want to redirect to a default image at public/errors/image-not-found.png
and terminate rewriting process. The other exceptions are files that end with .js, .css, .html
or .swf
. They also should only be served if they are located under the public/
folder or any other subfolders. If not, a simple 404-Not found
should be sent back. In either case of the last to the rewriting process need to stop of course.
Any other request should be redirected to index.php
and appended as a query string
. (even if the request points to a directory or to a file that is not under the conditions aforesaid, but exists, e.g: www.xyz.com/library/Database.php
-> www.xyz.com/index.php?url=library/Database.php
)
我有一半的措施解决办法:
这是我如何重定向一切的index.php:
I have half-measure solution:
This is how I redirect everything to index.php:
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
推荐答案
随着一系列的规则,也许应该模仿的流程图:
Following series of rules should probably mimic the flow-chart:
# for public folder pass through
RewriteRule ^public/.+?\.(?:jpe?g|ico|png|bmp|css|js|html|swf)$ - [L,NC]
# for other images
RewriteRule ^.+?\.(?:jpe?g|ico|png|bmp)$ /public/errors/img-not-found.jpg [L,NC,R=302]
# for other css|js|html|swf URIs
RewriteRule ^.+?\.(?:css|js|html|swf)$ - [L,NC,R=404]
# everything else, route to index.php
RewriteRule ^((?!index\.php).+)$ index.php?url=$1 [NC,QSA,L]
这篇关于Apache的重写模块,复杂的规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!