Deny access to php files but allow other php files in subdirectory

advertisements

I have a directory named "template". There are some php files that I don't want people have direct access to them, So I wrote this in htaccess :

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

Also there is some files that I want people have direct access to them, these files are named as:

switcher.php, switcher1.php, switcher2.php, switcher3.php, switcher4.php, switcher5.php

And located at template/styles/theme/

So I created a new htaccess file in that location and wrote this :

<FilesMatch "^switcher[1-5]?\.php$">
    Order Allow,Deny
    Allow from all
</FilesMatch>

This works good on my localhost, but when I upload the script on a cPanel Shared Host, it didn't work there. Access to all php files denied.

What can I do to fix this problem?

I can't ask hosting to make changes, because this script should installed on at least 30 shared hosts with different configurations!


Check for REQUEST_URI in each file:

$file = basename(__FILE__);
if(preg_match("/$file/", $_SERVER['REQUEST_URI'])){
    die('This file cannot be accessed directly!');
}