How To Deny Folder Access & Create Redirects Via .Htaccess On Your Server

Follow Us on Social Media

How To Deny Folder Access & Create Redirects Via .Htaccess On Your Server

How To Deny Folder Access & Create Redirects Via .Htaccess On Your Server



You must have previously heard about the .htaccess file if you are a web master or a web developer. If you've not heard about this important website file before, the .htaccess files or "distributed configuration files" provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.


Here in this post, I will be showing you ways to deny folder access on your web server, and how to create redirects in .htaccess file.

Why would you want to deny folder access?

You definitely don't want your visitors to access certain important files, pages or aspects of your website. You can deny this access via a .htaccess file.

So lets get started.

How to Deny Access to a Particular Directory Via Htaccess


To deny access to a particular directory, access your web server and create a file with the name ".htaccess" if this file does not already exist in the directory.

Edit the file and write the following code:

Deny from all

This denies all web request access to the folder.

How to Deny Access to a Particular Directory Via Htaccess Using RedirectMatch


To deny folder access using redirectmatch, add this line of code to the htaccess file:

RedirectMatch 403 ^/folder/?$

This code will limit access to the specific directory only and not sub-directories and files within it.

To also restrict access to sub-directories and files:


Modify the code to this:

RedirectMatch 403 ^/foloder/.*$


If url-rewriting-module is on, you can make use of mod-rewrite in your .htaccess file:

RewriteEngine on

RewriteRule ^folder/?$ - [F, L]

When you add this code to the htaccess file, all requests to the directory are forwarded to the forbidden error page.

How to Disable Listing of Directory Files Via Htaccess

When a web server lacks an index file, usually a listing of the files within the directory are shown when a request is made to the directory. 

You can disable this behaviour via the htaccess file.

To disable listing of your directory files or file structure in your web server, add this line of code in your htaccess file:

Options -Indexes

As simple as the code is, it does a lot!

How to Access only Certain Files in a Directory Via Htaccess

You can choose to limit access to all files in a directory, except the ones you whitelist.
In order to white list certain files of your desired extension while all others are blacklisted, you can add the following code to your htaccess file:

Order Allow,Deny
Deny from all

Using file match, we can now allow access to the files we want:

<FilesMatch> "\.(jpg|png|mp3|mp4|gif|php|css)$">
Order Deny,Allow
Allow from all
</FilesMatch>


 

How to Redirect from a Directory or Webpage to Another Webpage or Directory Via Htaccess

To redirect, you'd use the 'redirect' keyword

E.g to redirect to a 404 not found page, you can use the following code:

Redirect /uploads/ 404.php

This will redirect from the "uploads" directory to a 404 error page.

No comments



Powered by Blogger.