143

Here is the scenario:

  • There is a index.php file in root folder
  • some files are included in index.php which are in the includes folder.
  • 1 other file (submit.php) is in the root folder for form submit action.

I want to restrict direct user access to the files in includes folder by htaccess. also for submit.php. But include will work for index.php file. Like, if user types www.domain.com/includes/somepage.php, it will restrict it (may be redirect to a error page).

SomeKittens
  • 38,868
  • 19
  • 114
  • 143
Imrul.H
  • 5,760
  • 14
  • 55
  • 88

8 Answers8

280

I would just move the includes folder out of the web-root, but if you want to block direct access to the whole includes folder, you can put a .htaccess file in that folder that contains just:

deny from all

That way you cannot open any file from that folder, but you can include them in php without any problems.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • 7
    Will other file be able to make ajax request to the file present in that folder? – Chaitanya Chandurkar Apr 02 '13 at 13:22
  • 16
    @ChaitanyaChandurkar No, an ajax request is a normal http request so that will be denied. – jeroen Apr 02 '13 at 13:38
  • Sr b/c I new web programming. How can I add an exception for access to my file index.php? – PhatHV Aug 27 '15 at 02:12
  • I used deny from all and it restricted every url..not even showing login page.. :( – Aamir Jun 21 '16 at 06:34
  • @Aamir That is correct, no url in that folder will be accessible but you can include them in other files without any problems. – jeroen Jun 21 '16 at 06:35
  • I want to deny access to particular directory to show list of files in it in the browser. For example, If I go to the url, localhost/myproject/assets, it will show all the list of files in it, I want to deny that. And also if logged in user access specific file in it, for ex : localhost/myproject/assets/uploads/img/1.jpg then it should be accessible. Also how to deny access to a localhost/myproject/assets/uploads/img/1.jpg if that 1.jpg is uploaded by some other user. http://stackoverflow.com/questions/37874618/deny-access-to-directory-listing-using-htaccess – Aamir Jun 21 '16 at 06:41
  • @ChaitanyaChandurkar, if you modify .htaccess like this, then ajax requests are possible. The following is 4 lines: SetEnvIfNoCase X-Requested-With XMLHttpRequest ajax Order Deny,Allow Deny from all Allow from env=ajax – HOY Jan 01 '21 at 10:54
64

This is pure mod_rewrite based solution:

RewriteRule ^(includes/|submit\.php) - [F,L,NC]

This will show forbidden error to use if URI contains either /includes/ or /submit.php

Christian
  • 27,509
  • 17
  • 111
  • 155
anubhava
  • 761,203
  • 64
  • 569
  • 643
33

It's possible to use a Files directive and disallow access to all files, then use it again to set the files that are accessible:

<Files ~ "^.*">
  Deny from all
</Files>

<Files ~ "^index\.php|css|js|.*\.png|.*\.jpg|.*\.gif">
  Allow from all
</Files>
luke1985
  • 2,281
  • 1
  • 21
  • 34
  • 7
    I am not sure about the one who posted the question whether his problem solved or not, but this is the beauty of SO that multiple answer can help lots of members. This last answer resolved my issue. I want to let sites get css file but no access to ttf or otf fonts, and boom! resolved. – Moxet Khan Jun 27 '16 at 06:09
  • after this i am not abel to access files in subfolders – Gintare Statkute May 24 '17 at 13:31
16

1 liner mod_alias based solution :

RedirectMatch 403 ^/folder/file.php$

This will show forbidden error for /folder/file.php

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • 1
    +1 - This is actually very good, because no one can even see which files exist if you apply **404** to an entire folder, using regex `^folder`. – bytecode77 Jun 21 '16 at 19:08
10

If I understand correctly you just want to deny access to the includes folder?

An .htaccess with a 'DENY FROM ALL' directive placed in the includes folder would do the trick.

mainegreen
  • 237
  • 1
  • 2
  • 8
8

Your Q comes in two parts, both jeroen and anubhava's solutions work for part I -- denying access to /includes. anubhava's also works for part II. I prefer the latter because I use a DOCROOT/.htaccess anyway and this keeps all such control in one file.

However what I wanted t discuss is the concept of "denying access to submit.php". If you don't want to use submit.php then why have it in DOCROOT at all? I suspect that the answer here is that you use it as a action target in some forms and only want it to be fired when the form is submitted and not directly , e.g. from a spambot.

If this is true then you can't use anubhava's part II as this will cause your form to fail. What you can do here is (i) with the .htaccess check to ensure that the referrer was your own index page:

RewriteCond %{HTTP_REFERRER} !=HTTP://www.domain.com/index.php   [NC]
RewriteRule ^submit\.php$    -                                   [F]

And (ii) within your PHP index.php form generator include some hidden fields for a timestamp and validation. The validation could be, say, the first 10 chars of an MD5 of the timestamp and some internal secret. On processing the submit you can then (i) validate that the timestamp and validation match, and (ii) the timestamp is within, say, 15 minutes of the current time.

This you can prevent spamming as the only practical way that a spammer could get a valid timestamp / validation pair would be to parse a form, but this scrape would only have a 15 minute life.

TerryE
  • 10,724
  • 5
  • 26
  • 48
7

Depending on possible other options set at a higher level you may need to put the following in your .htaccess file in your includes directory:

Satisfy all
Order deny,allow
Deny from all

I ran into this when the upper directory defined basic authentication including the line:

Satisfy any

This was preventing my deny from all to take effect because the users were authenticated.

Keith
  • 924
  • 6
  • 14
3

You can add the below command to .htaccess file

Deny from all
ErrorDocument 403 "nothing is here"

It will display the "nothing is here" message in case of the unauthorised access.

If you want to redirect by an error code to a certain page then you can define a command as follows:

ErrorDocument 404 "/errors/404.html"

It will redirect to the /errors/404.html and show the custom page not found screen.

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
user1324491
  • 147
  • 4