Deny Access of Files,Folders through htaccess – Deny from All

How to Deny access of Files and Folders through .htaccess File

How to Deny access of Files and Folders through .htaccess File

How to restrict direct access of files and folders through .htaccess file. In this tutorial, I’ll discuss some .htaccess directives through which you can deny direct access of files and folders.

What is .htaccess File ?

.htaccess is a configuration file used by a web server (Apache). The fullform of htaccess is Hypertext Access. htaccess files are used to override server global configuration for the directory level in which you place this file.

.htaccess file contain directives, which is read by webserver (Apache) and apply them to directory level where this htaccess file is placed.

What’s the use of htaccess File

htaccess file is very useful when

i) You don’t have an access of httpd.conf file (Apache main configuration file) .
ii) You want to enforce some directory level configuration.

Imagine you have more than one website running on the same server and you want to configure different rules for each website. In that case, you won’t change global configuration (httpd.conf file) as it applies to both websites. In that case, .htaccess file is an ideal way to specify changes .

What’s the use of .htaccess File ?

There are a lot of things you can do through by using  the .htaccess file. Here’s the list of some useful things you can easily do by using the .htaccess file.

Password Protect a Folder through htaccess

Subscribe Our Tutorials

Get Latest Updates on Facebook

URL Rewriting – Create Seo-Friendly through htaccess

Prevent Image hotlinking

Leverage browser Caching

.htaccess Deny from All Directive- Disallow Direct Access of Files, Folders

To prevent direct access of all the files and folders of your project. Create .htaccess file in the root of your project directory structure. Then open the .htaccess file and write this directive deny from all.

Now you are done. If you want to check whether this directive is working or not. Try to access any file or folder of your project. You will get the following message.

How to Password Protect Folder through htaccess .

Deny Access to Certain File Types

Now you understand how to prevent direct access of all files and folders of a directory. But sometimes we need to prevent certain file types. Let’s understand how to prevent the access of certain file types.

Disallow all Files with .php Extension

Deny all Files with .inc Extension

Deny Single File through .htaccess

Suppose, I want to prevent an access of a specific file. Let’s take an example of config file which has all the system configuration. config.php is one example, you can include any PHP file. To disallow the access i write following lines on my .htaccess file.

2. Deny .htaccess file.

Prevent Any Direct Directory Browsing

Deny access to app folder.

Put this code in your htaccess file to prevent any direct directory browsing through browser.

There is lot of things which can be done through .htaccess files. Read How to leverage browser caching through .htaccess file

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , . Bookmark the permalink.