Playing with .htaccess

The fact that i have been playing with webhosting for a long time has made me like .htaccess very much , i dont care what others say , but to me the file is indispensible and let me show you how small tasks can be easily achieved by this one file wonder. first thing first , make a test .htaccess file in a test hosting account and proceed. For those who might be confused , .htaccess is a file by the same name.

1. htaccess file redirecting –

.htaccess can be used to make redirects in your site , and that too very easily and these redirects work much better than javascript redirects my experience as a webmaster has taught me.

See and Observe

Redirect /toberedirected http://wisetechie.com/blog/

Here tobe redirected is the old directory which needs to be redirected to the blog directory.

For example – Redirect /tests http://wisetechie.com/blog/

will take you to my blog directory upon entering http://wisetechie.com/tests , the change will take place automatically in the address bar

2. Banning IPs through htaccess-

Although most control panels come with this banning IP feature built-in , some dont , to ban an IP , paste the following into your .htaccess file

deny from IP address

I tried using my own IP address , getting the forbidden error was bit of excitement for me , but being able to access backend easily meant that i could easily edit .htaccess again , i would however reccomend only to ban rogue IPs which you know are trouble.

I have personally used this command , once there was an IP from china which was basically a bot , reading my blog’s feed and copying all content for an automated site , now all it gets is a 403 forbidden page , but it keeps trying , atleast my content is secure. Later i got that IP address’s traffic null routed which basically ended the headache.

3. Changing Directory Index through htaccess-

This is a good tool , however DONT use it if you use automated Installation scripts as they themselves sometimes edit this information and it might result in a conflict. What does this command do then ?

This command allows you to change the default index file of your site , for example , typing yoursite.com will make the webserver originally show yoursite.com/index.html , what if i dont want index.html but wisetechie.html , i can change that through .htaccess too.

DirectoryIndex wisetechie.html wt.html wt.php

The command basically sets the priority for the directory indexes through the .htaccess file , if i open mysite.com/abc/ , then first the server will search for wisetechie.html in that directory , then wt.html in that directory and if even that is not found it will search for wt.php

4. Make Files Secure using htaccess

Since we are learning old school , another thing we can do easily is protect certain files or certain class of files from being viewed by external agents , an easy way to do this is by the following syntax

# To protect .doc files

<Files *.doc>

Deny From All

</Files>

The command gives a 403 forbidden error for every .doc file that is tried to be accessed by the users.

5. Banning Rogue Bots through htaccess

All bots come with user agents , some bad bots can be easily identified by their user agents and can be blocked and can help you save precious bandwidth and inturn precious money. We will discuss this in a later post , just to keep you hungry for some more.

6. Index Ignore Command in htaccess file –

This command again is the old school way to prevent directory listings , if i have a directory full of family images , i dont want any stranger to be able to come and list all the pictures namewise and take his pic , only people whom i have given specific URLs should be able to view my personal images.

IndexIgnore *

Put this in the .htaccess file of the directory you dont want anyone to be able to see , if you want specific files not to be listed you can also do that by the following modification.

IndexIgnore *.jpeg *.jpg

This particular command will let all files other than .jpg or .jpeg extensions to be listed in the directory listings as and when done by the user on my site.

The Purpose –

Although most of these are automated these days by programs such as Cpanel and other such systems , i felt it is necessary to know these as this raw knowledge is important for all webmasters , when you add a redirect or a deny entry in cpanel , you should know what is the adventurous way of doing it through your good old htaccess files. By compiling just a few of these commonly used commands i hope i have aroused interest in enough individuals as to how web site administering is actually done. 🙂

Now when you are done with reading my htaccess tutorial you must realise this is just the basics of htaccess, the vegetables for the roasted vegetables (ok , i know , bad joke , i mean htaccess can be used as a foundation for bigger things) , you got to put the bits and pieces together to create a masterpiece. For those of you wondering what can we do with a .htaccess file , Here is something Rohit cooked up for us(using htaccess and php) over at his blog

Just goes to show a little programming + a smart brain can make small and witty things which you wouldnt have imagined in your wildest dreams otherwise.

Now you guys have probably seen that the avatars on this forum are static, they have to be manually changed before you can use a new one. :-?
Now take a look at my avatar url
http://www.smart-techie.com/images/avatar.gif
Now what happens when you view it? You see a jpg or a gif image.
Try accessing the same url 5 or more times… Did you notice something?
The picture changes automatically at random (amongst a set of 3 pics) If you didn’t see this try more times ;)
How does it happen?
The first part is a VERY simple php script on my webserver (you need a hosting account with php scripting enabled for running scripts). Also (in Linux) CHMOD the files to give execute permission to everyone.
The php code that I am using is

php
$random = rand(1,3);

switch ($random)

{
case 1:header(“location: /images/konqi.jpg“ ) ;

break;

case 2:header(“location: /images/Homer.gif“ ) ;

break;

case 3:header(“location: /images/Hobbes.gif“ ) ;

break;
}

header(‘Cache-Control: no-store, no-cache, must-revalidate‘) ;

header(‘Cache-Control: post-check=0, pre-check=0‘, FALSE) ;

header(“Expires: Mon, 26 Jul 1997 05:00:00 GMT“ ) ; // Date in the past

header(‘Pragma: no-cache‘) ;

?>

Doesn’t take a genius to figure out what is going on here. Anyways I’ll explain :)
1. A random number is chosen between 1 and 3 and the corresponding image is displayed. This is done by the header function which is used to send HTTP headers ( a temporary redirect HTTP 302 response) This says that the resource that you want to see is at some other location).2. The headers at the end are DESPERATE ones which tell the browser “Please don’t cache the images or you will spoil my image changing effect?” Now to save bandwidth and load pages faster, browsers store copies of resources that they retrieved from the Internet on your disk in a location called cache/Temporary Internet files or similar location. Try about:cache in mozilla to see whats in it.
Also an expiry date is given in past so that the browser thinks “Oh, this content that I retrieved has become old. I shouldn’t cache it. Next time I’ll ask for a fresh copy” Sweet…
Now the file is saved as somename.php on your server and on running it the effect of changing avatars is complete.
2. Now for the final bit. Try pasting this url in your avatar field and the forum complains. It wants the URL to end in gif/jpg or whatever. So how to bypass this? Simple. Use the Apache .htaccess file (not available on IIS)
.htaccess is a file that stores settings on a folder basis on you webserver. I modify this file to add a statement

Redirect /avatar.gif http://www.website.com/images/somefile.php

There you have it! It is always easy to fool software.
Try this at least once to see the power of web scripting.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.