itoctopus
itoctopus

Reputation: 4261

Allowing Access to Only a Specific Path in .htaccess

I have the following in my .htaccess file:

<Files *.php>
    Order Allow,Deny
    Deny from all
</Files>

<Files index.php>
    Order Allow,Deny
    Allow from all
</Files>

The above code is to restrict access to only the index.php file. However, the problem is that if there is another index.php file under a subdirectory, then that index.php file is allowed to get executed as well. This is not what I want. I want something like:

<Files /home/user/public_html/index.php>
    Order Allow,Deny
    Allow from all
</Files>

and

<Files /home/user/public_html/admin/index.php>
    Order Allow,Deny
    Allow from all
</Files>

(I just want 2 index.php files to be executed)

But the above doesn't work, as it'll deny the execution of the should-be-allowed index.php (again, there are two that should be allowed, one under the root directory of the website, and another under the admin folder).

Does anyone have any tips on how to do this?

Upvotes: 2

Views: 3265

Answers (1)

anubhava
anubhava

Reputation: 785481

Use a mod_rewrite rule with regex support to allow only /index.php OR /admin/index.php:

RewriteEngine On

RewriteRule ^(?!(admin/)?index\.php$).+?\.php$ - [F,NC]

Files or FilesMatch doesn't work with paths but individual filenames.

Upvotes: 2

Related Questions