Excluding files from htaccess rewrite rules

若如初见. 提交于 2019-12-30 07:09:10

问题


I have an htaccess rewrite setup in my PHP application to route files via the bootstrapper file. 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. All was working well...

That is, until I started doing ajax-y stuff. I'm routing all my ajax queries through a single file, ajaxDispatcher.php. When I did that, the htaccess (correctly) caught the request and used my bootstrapper to route it inappropriately. Similarly, it was attempting to route unwanted files such as .ico, .css, etc.

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. 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?


回答1:


You should move your new rule atop the RewriteCond block:

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

Otherwise the RewriteConditions don't cover the extensions RewriteRule anymore, for which I assume they are intended.

Another alternative is turning it into another RewriteCond of course:

RewriteCond %{SCRIPT_NAME}  !ajaxDispatcher

Or injecting it as assertion into the final 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




回答2:


If you remove the ajaxDispatcher.php line from the code you show, it should work - because of the !-f and !-d rules, requests to files that actually exist will be excluded from the rewrite ruke.



来源:https://stackoverflow.com/questions/8609735/excluding-files-from-htaccess-rewrite-rules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!