John
John

Reputation: 7880

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

I have a .htaccess file in website root with the following rules :

Options -MultiViews
Options All -Indexes

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

And another .htaccess file in a sub-directory (let's call it Foo) :

Order Allow,Deny
Deny from all

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

When I access for example : /test/test/test it normally redirects me to index.php, but when I my URI starts with existing directory it throws 403 error, ex : /Foo/test/test/test => 403 Error

So my question is : How to make it to redirect to index.php even if it matches an existing sub-directory?

Note : Sub-Directoy must contain a .htaccess file to disallow direct access to files in it.

Another Question : How to make all requests (even existing files and directories) except (images, css, js) to go to index.php?url=$1? When i remove RewriteCond %{REQUEST_FILENAME} !-f it redirects to index.php?url=index.php&url=$1 or gives Internal Error

Upvotes: 1

Views: 4567

Answers (1)

anubhava
anubhava

Reputation: 785128

Keep your root .htaccess as this:

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]

Keep your /Foo/.htaccess as this:

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 will inherit rules from parent .htaccess
  • Instead of allow/deny better to use mod_rewrite for finer control here

Upvotes: 2

Related Questions