« Links open in new window/tab »

« Links open in new window/tab »

Web Design & Development

Monitor Page for Changes


It's private by ChangeDetection
Share |
Page copy protected against web site content infringement by Copyscape

.htaccess Commands, Part II
PayneLess Designs

In Part I you were introduced to .htaccess and some of its useful features. In this part you will be shown how to use the .htaccess file to implement some of these.

Stop A Directory Index From Being Shown:

Sometimes, for one reason or another, you will have no index file in your directory. This will, of course, mean that if someone types the directory name into their browser, a full listing of all the files in that directory will be shown. This could be a security risk for your site.

To prevent against this (without creating lots of new 'index' files, you can enter a command into your .htaccess file to stop the directory list from being shown:

Options - Indexes
Deny/Allow Certain IP Addresses

In some situations, you may want to only allow people with specific IP addresses to access your site (for example, only allowing people using a particular ISP to get into a certain directory) or you may want to ban certain IP addresses (for example, keeping disruptive members out of your message boards). This will only work if you know the IP addresses you want to ban. Today, most people on the internet have a dynamic IP address, and using this method is not always the best way to limit usage.

You can block an IP address by using:

deny from 000.000.000.000

where 000.000.000.000 is the IP address. If you only specify 1 or 2 of the groups of numbers, you will block a whole range.

You can allow an IP address by using:

allow from 000.000.000.000

where 000.000.000.000 is the IP address. If you only specify 1 or 2 of the groups of numbers, you will allow a whole range.

If you want to deny everyone from accessing a directory, you can use:

deny from all

but this will still allow scripts to use the files in the directory.

Alternative Index Files:

You may not always want to use index.htm or index.html as your index file for a directory, for example if you are using PHP files in your site, you may want index.php to be the index file for a directory. You are not limited to 'index' files though. Using .htaccess you can set foobar.yada to be your index file if you want to!

Alternate index files are entered in a list. The server will work from left to right, checking to see if each file exists. If none of them exist, it will display a directory listing (unless, of course, you have turned this off).

DirectoryIndex index.php index.php3 messagebrd.pl index.html index.htm
Redirection:

One of the most useful functions of the .htaccess file is to redirect requests to different files, either on the same server, or on a completely different web site. It can be extremely useful if you change the name of one of your files but allow users to still find it. Another use (which is very useful) is to redirect to a longer URL, for example, you can use a very short URL for your affiliate links. The following can be done to redirect a specific file:

Redirect /location/from/root/file.ext http://www.othersite.com/new/file/location.xyz

In this above example, a file in the root directory called oldfile.html would be entered as:

/oldfile.html

and a file in the old subdirectory would be entered as:

/old/oldfile.html

You can also redirect whole directories of your site using the .htaccess file. For example: If you had a directory called olddirectory on your site and you had set up the same files on a new site at:

http://www.newsite.com/newdirectory/

you could redirect all the files in that directory without having to specify each one:

Redirect /olddirectory http://www.newsite.com/newdirectory

Then, any request to your site below /olddirectory will be redirected to the new site, with the extra information in the URL added on. For example if someone typed in:

http://www.youroldsite.com/olddirecotry/oldfiles/images/image.gif

They would be redirected to:

http://www.newsite.com/newdirectory/oldfiles/images/image.gif

This can prove to be extremely powerful if used correctly.

Part 3:

In part 3 you will cover a few other uses of the .htaccess file including password protection.

If you have found any services on my site to be beneficial and would like to donate, please feel free to donate ANY amount you want. Thank you very much.

Return to Top

[an error occurred while processing this directive]

Valid XHTML 1.0 StrictValid CSS!

Valid Robots.txt