Deny direct access to all .php files except index.php
Solution 1:
Are you sure, you want to do that? Even css and js files and images and ...?
OK, first check if mod_access in installed to apache, then add the following to your .htaccess:
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
<Files /index.php>
Order Allow,Deny
Allow from all
</Files>
The first directive forbids access to any files except from localhost, because of Order Deny,Allow
, Allow gets applied later, the second directive only affects index.php.
Caveat: No space after the comma in the Order line.
To allow access to files matching *.css or *.js use this directive:
<FilesMatch ".*\.(css|js)$">
Order Allow,Deny
Allow from all
</FilesMatch>
You cannot use directives for <Location>
or <Directory>
inside .htaccess files, though.
Your option would be to use <FilesMatch ".*\.php$">
around the first allow,deny group and then explicitely allow access to index.php.
Update for Apache 2.4:
This answer is correct for Apache 2.2. In Apache 2.4 the access control paradigm has changed, and the correct syntax is to use Require all denied
.
Solution 2:
Actually, I came here with the same question as the creator of the topic, but none of the solutions given were a complete answer to my problem. Why adding a code to ALL the files on your server when you could simply configure it once ? The closest one was Residuum's one, but still, he was excluding ALL files, when I wanted to exclude only php files that weren't named index.php.
So I came up with a .htaccess containing this :
<Files *.php>
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</Files>
<Files index.php>
Order Allow,Deny
Allow from all
</Files>
(Remember, htaccess files are working recursively, so it suits perfectly the prerequisite of the question.)
And here we go. The only php files that will be accessible for an user will be the ones named index.php. But you can still acces to every image, css stylesheet, js script, etc.
Solution 3:
An oblique answer to the question is to write all the code as classes, apart from the index.php files, which are then the only points of entry. PHP files that contain classes will not cause anything to happen, even if they are invoked directly through Apache.
A direct answer is to include the following in .htaccess:
<FilesMatch "\.php$">
Order Allow,Deny
Deny from all
</FilesMatch>
<FilesMatch "index[0-9]?\.php$">
Order Allow,Deny
Allow from all
</FilesMatch>
This will allow any file like index.php, index2.php etc to be accessed, but will refuse access of any kind to other .php files. It will not affect other file types.
Solution 4:
You can try defining a constant in index.php
and add something like
if (!defined("YOUR_CONSTANT")) die('No direct access');
to the beginning of the other files.
OR, you can use mod_rewrite
and redirect requests to index.php, editing .htaccess
like this:
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L,R=301]
Then you should be able to analyze all incoming requests in the index.php and take according actions.
If you want to leave out all *.jpg, *.gif, *.css and *.png files, for example, then you should edit second line like this:
RewriteCond $1 !^(index\.php|*\.jpg|*\.gif|*\.css|*\.png)