What is .htaccess

This is a file, which all of us, who use cPanel hosting have on our account. Most of you have probably wondered what is this and how do I use it. I will try to explain it below and provide a few usefull examples on how to use its great capabilities.

What is the .htaccess?

This is a part of the Apache web server configuration. It can be best summarized as an extension of the web server configuration applying to your account only. It is usually empty as everything needed for the smooth operation of your account is already configured in the main webserver configuration files. The great thing about Apache though, is that it has an option to extend this configuration, so more specific settings can be applied per account basis. Basically every feature in cPanel, which sounds very unfamiliar is controlled/applied using .htacccess.

With it, you can password protect a directory, redirect visitors, hide the content of a directly, etc. Below I will describe some of its common features:
1. as shown earlier, using .htaccess, you can redirect visitors to various locations.

2. using .htaccess, we can also mask URLs and make them SEO friendly.

Most PHP/MySQL based applications have such feature by default. Such examples are Joomla, Drupal, WordPress.  Lets say you have a PHP based shop, which generates long URLs, which make no sense at all. Something like:

http://your-domain.com/shop.php?product=44

For SEO purposes and of course for a more professional appeal, you can change this URL to:

http://your-domain.com/cat/product/44 with this short rewite rule:

RewriteEngine on
RewriteRule ^product/([^/\.]+)/?$ product.php?id=$1 [L]

3. .htaccess can be also used to arrange your site/directory indexes.

By default, Apache uses the following priority for index files in a directory:

index.html index.htm index.php index.phtml

As you noticed, all are named index and all are in lower case characters. One solution for you is t simply comply with this and rename your files. This however can sometimes lead to broken links or even a broken site. It is much easier to add your own index priority, which can contain upper case characters and different file names to suit your site. This can be done with the following directive:

DirectoryIndex Index.php default.html Home.htm

You can arrange them any way you need, just remember to place your index file first :)

4. Use .htaccess to disable directory index and listing

If you have sensitive data in a folder and do not wish to have random people list it, you can easily disable directory listing for a selected folder. Simply add the following to the .htaccess in the folder in question:

Options -Indexes

5. Use .htaccess to deny access.

a very useful option if there are a bunch of unwanted visitors to your website is to deny them access using .htaccess. All you need to know are their IP addresses. Usually your software (forum, portal or a shop) will have this data for you. Then, simply add the following to .htaccess:

allow from all
deny from 12.12.12.12
deny from 13.13.13.13

This will allow acces to your site to everyone (all), but will then deny access from both IP addresses. The same can be used in reverse order if you wish to deny everyone access and only allow yourself in:

deny from all
allow from 10.10.10.10

This way everyone will see a forbidden message, while you will be able to open your pages. It can sometimes be useful while developing a website.

Bookmark and Share

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark
tabs-top  banner ad


Leave a Reply