mod_rewrite: Redirect if anything but a file

扶醉桌前 提交于 2020-01-15 06:20:07

问题


I have an Apache .htaccess file in the parent directory to directory this .htaccess file is currently located. This works perfectly redirecting all requests as required. However, I would like it so in this directory, if the request is for a valid file (and NOT a directory), ignore the main rewrite rule.

Currently, the following turns off the rewrite for directories as well, and I can't figure out why.

<IfModule mod_rewrite.c>
 Options +FollowSymLinks
 Options +Indexes
 RewriteEngine On
 RewriteBase /
 RewriteCond %{REQUEST_URI} !-d
 RewriteCond %{REQUEST_URI} -f
 RewriteRule . - [L]
</IfModule>

Thanks to anyone who contributes.


回答1:


Try REQUEST_FILENAME instead of REQUEST_URI:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]

REQUEST_URI does only contain the URI path but Apache needs an absolute file system path to test agains -d and -f. And REQUEST_FILENAME holds the absolute file system path corresponding to the current requested URI.



来源:https://stackoverflow.com/questions/1399090/mod-rewrite-redirect-if-anything-but-a-file

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