简体   繁体   English

从htaccess重写规则中排除文件

[英]Excluding files from htaccess rewrite rules

I have an htaccess rewrite setup in my PHP application to route files via the bootstrapper file. 我在PHP应用程序中有一个htaccess重写设置,用于通过bootstrapper文件路由文件。 In essence, the goal is to take a URL such as www.domain.com/view/key/value/key/value where the view would route accordingly and the key/value pairs would be available via a function I wrote in the bootstrapper to the views. 从本质上讲,目标是获取一个URL,例如www.domain.com/view/key/value/key/value,其中视图将相应地路由,并且键/值对将通过我在引导程序中编写的函数可用对观点。 All was working well... 一切都运作良好......

That is, until I started doing ajax-y stuff. 也就是说,直到我开始做ajax-y的东西。 I'm routing all my ajax queries through a single file, ajaxDispatcher.php. 我通过单个文件ajaxDispatcher.php路由我的所有ajax查询。 When I did that, the htaccess (correctly) caught the request and used my bootstrapper to route it inappropriately. 当我这样做时,htaccess(正确)捕获了请求并使用我的引导程序不适当地路由它。 Similarly, it was attempting to route unwanted files such as .ico, .css, etc. 同样,它试图路由不需要的文件,如.ico,.css等。

I figured out the file extension routing exception, however, I've not been able to have .htaccess ignore rewrite rules for the single file ajaxDispatcher.php. 我想出了文件扩展路由异常,但是,我无法让.htaccess忽略单个文件ajaxDispatcher.php的重写规则。 Here's where my code stands: 这是我的代码所在的位置:

 RewriteEngine On
 RewriteBase /
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^/ajaxDispatcher.php$ $0 [L]
 RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php?route=$1 [L]

How do I get .htaccess to ignore the routing rules only for ajaxDispatcher? 如何让.htaccess忽略仅针对ajaxDispatcher的路由规则?

You should move your new rule atop the RewriteCond block: 您应该在RewriteCond块上移动新规则:

RewriteRule ^/?ajaxDispatcher.php$ - [L]

Otherwise the RewriteConditions don't cover the extensions RewriteRule anymore, for which I assume they are intended. 否则,RewriteConditions不再包含RewriteRule扩展,我认为它们是有意的。

Another alternative is turning it into another RewriteCond of course: 另一种选择是将其转换为另一个RewriteCond:

RewriteCond %{SCRIPT_NAME}  !ajaxDispatcher

Or injecting it as assertion into the final RewriteRule (?!ajaxDispatcher.php) . 或者将其作为断言注入最终的RewriteRule (?!ajaxDispatcher.php)

The ordering thing is best explained on Serverfault: https://serverfault.com/questions/214512/everything-you-ever-wanted-to-know-about-mod-rewrite-rules-but-were-afraid-to-as 在Serverfault上最好地解释订购事项: https ://serverfault.com/questions/214512/everything-you-ever-wanted-to-know-about-mod-rewrite-rules-but-were-afraid-to-as

如果从显示的代码中删除ajaxDispatcher.php行,它应该可以工作 - 由于!-f!-d规则,对重写ruke中的实际存在的文件的请求将被排除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM