0

How can I write a .htaccess script that prevents the user from running .php files in a certain directory and it's child directories?

I would need to be able to include the php file from the servers index.php or other files though, basically I want to enable to the server to to include the php files from a certain directory but, if the user were to type in the specific URL, it would deny access.

2
  • 3
    Keep them outside DOCUMENT_ROOT
    – tereško
    Commented Dec 5, 2013 at 6:41
  • 1
    You have something of a dichotomy in your title. The user requesting them is the server
    – user2363448
    Commented Dec 5, 2013 at 6:42

2 Answers 2

1

This could do the trick

<FilesMatch "\.php$">
   Deny from all
<FilesMatch>

Also, see the answers to this question: Deny direct access to all .php files except index.php

2
  • Thanks Dmitry, fixed it.
    – naivists
    Commented Dec 5, 2013 at 6:48
  • Sorry for my previous comment, I was mistaken - it is possible to use File directive in .htaccess. I'v just looked up Apache documentation and checked <Files "*.php"> variant - it works just fine. Commented Dec 5, 2013 at 6:53
0

I usually do something like this:

If I want a user to access a file, say members.php or index.php

<?php
    $GLOBAL_PAGE="page name"; //set the 'visible' flag
    include_once 'headers.php'
...
?>

In headers.php:

<?php
     if(!page_is_authorized($GLOBAL_PAGE))
         //redirect to a valid page
         header( 'Location: index.php' );
     ....
?>

where page_is_authorized($s) checks $s against a list of valid values.

I know this doesn't use .htaccess rules as you requested but I usually don't have access to that file so there goes. It's portable too!

1
  • +1 about the portability. This helps if for some reason using .htaccess is forbidden on the server.
    – naivists
    Commented Dec 5, 2013 at 11:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.