.htaccess 403如果url包含带有另一个htaccess文件的目录,则禁止使用


.htaccess 403 Forbidden if url contains directory with another htaccess file

我在网站根目录中有一个.htaccess文件,具有以下规则:

Options -MultiViews
Options All -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA]

子目录中的另一个.htaccess文件(我们称之为Foo):

Order Allow,Deny
Deny from all
<files ~ "'.(js|jpe?g|png|gif|bmp|css)">
    Allow from all
</files>

例如,当我访问:/test/test/test时,它通常会将我重定向到index.php,但当我的URI以现有目录开始时,它会抛出403错误,例如:/Foo/test/test/test=>403错误

所以我的问题是:如何使其重定向到index.php,即使它与现有的子目录匹配

注意:Sub Directoy必须包含.htaccess文件才能禁止直接访问其中的文件。

另一个问题:如何让除(images,css,js)之外的所有请求(甚至是现有的文件和目录)都转到index.php?url=$1当我删除RewriteCond %{REQUEST_FILENAME} !-f时,它重定向到index.php?url=index.php&url=$1或给Internal Error

保持root.htaccess如下:

Options All -Indexes -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.*)$ [NC]
RewriteCond %{REQUEST_URI} !(^/index'.php|'.(js|jpe?g|png|gif|bmp|css))$ [NC]
RewriteRule ^ /index.php?url=%1 [QSA,L]

保持你的/Foo/.htaccess如下:

RewriteEngine On
RewriteOptions Inherit
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !'.(js|jpe?g|png|gif|bmp|css)$ - [F,NC]
  • RewriteOptions Inherit将继承父级.htaccess的规则
  • 此处最好使用mod_rewrite进行更精细的控制,而不是allow/deny