November 16, 2012

Block access to files and sub directory with .htaccess?

Question by jaya malladi

I have nearly 30 php files, 4 sub directories in my directory. I want to block some php files and sub directories from user in direct viewing like

My currect htaccess file

## Enable Mod Rewrite, this is only required once in each .htaccess file
RewriteEngine On 
RewriteBase / 
## Test for access to includes directory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /includes/ .*$ [NC] 
## Test that file requested has php extension 
RewriteCond %{REQUEST_FILENAME} ^.+.php$ 
## Forbid Access 
RewriteRule .* - [F,NS,L]

How to do it?

Answer by Starx

You can filter the files like this

<Files ~ ".php$">
  Deny from all

And directory like this

<Directory "/subdirectory">
  Deny from all

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!