.htaccess is a word which every developer has heard. But they skip it because some think its too hard to understand. But I am sure nothing is hard for a good programmer. Well .htaccess is a file which is used to define behaviour of your directories and server request to those directories or files. This file is supported by apache and not IIS. This is an ASCII file and should be uploaded to the server using ASCII mode and not binary.

Workarea for .htaccess

If we place .htaccess file into the root of our website then all the folder and subfolders will be affected by the rules written in it. But if the subfolder or folders have their own .htaccess file (may be empty) then the first preference is given to it. So the preference is for the nearest .htaccess file.

I will now list some of the basic .htaccess techniques which can be used to makea programmer’s life easy.

Redirect Url

Redirect /mydir/oldfile.html http://mydomain.com/newdir/newfile.html

Stop Indexing a directory

Yes this is one the tedious task which every programmer has to do. So that the file are not indexed when a directory may get called from browsers. A simple one line rule can make your life easy.

Options -Indexes

Chanding the index page of the directory

We all know that by default Apache looks for the file index.html in a directory when it gets a request from the client. What if we want to change the index file, i may want my index.php to be parsed and send to the client as a response. Then i can simply write the following rule in .htaccess

DirectoryIndex index.php index.html

The priority decreases from left to right

Adding SSI Support to the server

Many people want to use SSI (Server Side Includes) but their website hosting providers doesn’t seem to give support on that. Well no worries, .htaccess have got an answer for the desperadoes 😛

AddType text/html shtml
AddHandler server-parsed .shtml
Options Indexes FollowSymLinks Includes
you may also want to change the default index file, this can be done using
DirectoryIndex index.shtml index.html

A quick explanation would be, that the first line adds the type of extension and second line ask the file of the type shtml to be parsed on the server side. Third line is not always mandatory as long as your server has enabled it. FolloWSymLinks is basically tells apache to follow the real file if it finds a link to the real file in the specified directory.

Block Users based on IP address

There are times when you want to block certain users or certain range of IP address to be blocked, so that they cannot access your website or pages/dirs/. This can be done using the following code.

order allow,deny
deny from
deny from
allow from all

The First Line defines the three phase parsing of apache. It is not necessary to define this here but still a quick definition would be , order allows to parse the allow directives first then deny conditions and then the conditions which do not match both of them and then sends back the response to the client. Second line denies the access from the given IP, Third line is similar to second, while fourth line allows everyone else to access.

We could also write

 deny from all

to block everyone to access the given directory on apache.

Well guys, will write more of these snippets very soon.

Gotta go now 😛

Cheers !!

Realin !

Share this post: