How to Redirect a Specific Folder to a Custom Web Page

How to Redirect a Specific Folder to a Custom Web Page

If you have a self hosted site, chances are that you’ve used your hosted space as a store for your files at some time or the other. Maybe it’s an FTP dump for some unrelated information, or it could be a set of photographs or whatever. You most likely don’t want any of this to be accessible to the general public via a simple HTTP request. But simply generating a 403 Forbidden error looks unfriendly. Moreover, even with directory browsing disabled, a user might be able to read the internal files if they know the filename and are able to access it directly.

What we want is complete redirection of a specific folder and the files within it. In this tutorial, I went to show you how to use .htaccess to redirect all requests made to a specific folder or the files within it to a useful page (or search result) on your site that allows them to continue browsing your site without interruption.

Using 301 and 302 Redirects

A complete explanation of different types of redirects is out of place here. Let’s just say that if you intend for this to be permanent – that is you never wish to use the original URL again, then a 301 redirect is the best option. The redirect type is not for visitors, but for other agents like search spiders. If this is temporary, use a 302 redirect as shown below.

Creating and Disallowing the Dummy Page

I start off by creating a directory and skeleton HTML page. You can in see the screenshot below, that I’ve created one called “testfolder” with a file inside it named “testpage.html”.

testfolder created with file inside

This directory resides in the same location as my site installation along with the other core folders. As expected, I can access the HTML file by simply typing in the URL as shown below:

accessing the file directly

By default, directory browsing is disabled so a person will get a 403 forbidden error if they try and list the contents of “testfolder”. But what I want to do is to redirect any HTTP request for the contents of the folder or to the folder itself. I want this request to be diverted to my site – either a maintenance page, or a search page, or some other resource with useful information.

So the first step is to decide where you want to redirect to. You can see in the screenshot below, that I’ve created a separate page for this:

useful page

To create the redirection, open up your .htaccess file in the root folder of your installation. At the end, insert the following code.

RewriteCond %{REQUEST_URI} ^/testfolder/.*
RewriteRule (.*) http://www.webhostingshed.com/uncategorized/some-useful-page.html/ [R=301,L]

Replace the bolded “testfolder” with your own directory name. Then change the other URL to the useful page that you have in mind. Finally if this is a permanent arrangement, leave “301” as is. Otherwise change it to 302.

This simply creates a rewrite condition specifying that any request made to the “testfolder” directory gets redirected to the new URL. The “L” parameter specifies that all further execution must come to a halt. After saving the .htaccess file if I try and access my test folder once again, I get the following result:

page redirected

 You can see that it’s been redirected to the new URL I just created via a “301 Moved Permanently” status message.

Using a simple redirect like this, you can prevent your users from seeing an unfriendly 403 forbidden error message and direct them to another page either on your site, or somewhere else. The type of redirection – 301 or 302 – will depend on whether or not this change is permanent.

Leave a Reply

This Website is Hosted by

Powered by RamNode

Disclosure: We receive a compensation from some of the companies whose products or services are presented on our website.