Fun with .htaccess


In a web server, the server’s configuration file normally defines the settings that files and directories in your account follows. However these settings can be customized to an extend by yourself using simple text files called .htaccess. When properly congfirued, .htaccess file can allow you to ,etc. Each line in a .htaccess file is called a directive. Directives are applied to the directory the .htaccess file resides in and any subdirectories within that directory. So if it is placed in the public_html directory, all the files and directories in your account will be affected.

Creating a .htaccess file:

You can create a .htaccess file using any text editor like Notepad (Windows), TextEdit (Mac), vi (Linux), etc.. To create one, open your text editor, enter a few directives and save the file as .htaccess (with the period). If your editor won’t let you save the file as .htaccess, name it htaccess.txt instead. You can rename it after you’ve uploaded it to your server. When uploading the .htaccess file to your server, make sure that it is uploaded in ASCII mode. Some FTP clients may not show the file as files with names that begin with a period are normally considered as hidden files. So you will have to change the settings in your FTP client in that case. If there is an existing .htaccess file, you can just edit it with the required directives.

Even though most of the tweaks can be done using cPanel which uses .htaccess file, a basic understanding of various directives used in .htacess file will give you more control. We will cover some useful stuff that we can achieve using a .htaccess file in this blog:

1) Redirect Traffic:

You can redirect all visitors or certain visitors only based on their IP address, etc.

order deny,allow
deny from all
allow from

ErrorDocument 403 /page.html

allow from all

Replace with your IP address. Also replace page.html with the name of the page you want visitors to see.

2) Custom Error Pages:

The web server normally displays a 404 error when it receives a request for an non existant page. You can replace the server’s default error page with one of your own that explains the error in plain language and links visitors to your home page. Here’s how to use your own page:

ErrorDocument 404 /404.html

Replace 404.html with the name of the page you want visitors to see.

3) Redirect Moved pages:

When you move your pages to a new location, the URLs in the index of search engines will not get updated and you will loose traffic when visitors land in the old URL. You can use a 301 redirect to automatically send to the new page when they try to access the old one:

Redirect 301 /old.html

Using a 301 redirect also ensures the page doesn’t lose its search engine ranking.

4) Prevent Directory Browsing:

When there’s no index page in a directory, visitors can look and see what’s inside. This may be insecure as you are exposing hidden content to the public. Some servers are configured to prevent directory browsing like this. If yours isn’t, here’s how to set it up:

Options All -Indexes

5) User Friendly URLs

This one is a bit tricky, but very useful and helps in Search Engine Optimization as well.

For example, your page is at the location:

When it comes to URLs, as long as the meaning is clear, shorter is always better for your visitor and a search bot, like:

With htaccess and an Apache module called mod_rewrite, you can set up URLs however you want. Your server can show the contents of “/html/new/contact.html” whenever anyone visits “”. Here are a few examples:

RewriteEngine on
RewriteRule ^contact/$ /html/new/contact.html [L]

These are some of the basic tweaks that you can achive using a .htaccess file. You can find many advanced directives here.