Htaccess NOtes
Htaccess NOtes
htaccess
Introduction
In this tutorial you will find out about the .htaccess file and the power it has to improve your
website. Although .htaccess is only a file, it can change settings on the servers and allow you to
do many different things, the most popular being able to have your own custom 404 error
pages. .htaccess isn't difficult to use and is really just made up of a few simple instructions in a
text file.
A good sign of whether your host allows .htaccess files is if they support password protection of
folders. To do this they will need to offer .htaccess (although in a few cases they will offer
password protection but not let you use .htaccess). The best thing to do if you are unsure is to
either upload your own .htaccess file and see if it works or e-mail your web host and ask them.
There is a huge range of things .htaccess can do including: password protecting folders,
redirecting users automatically, custom error pages, changing your file extensions, banning users
with certian IP addresses, only allowing users with certain IP addresses, stopping directory
listings and using a different file as the index file.
".htaccess"
(including the quotes). If this doesn't work, you will need to name it something else (e.g.
htaccess.txt) and then upload it to the server. Once you have uploaded the file you can then
rename it using an FTP program.
Warning
Before beginning using .htaccess, I should give you one warning. Although using .htaccess on
your server is extremely unlikely to cause you any problems (if something is wrong it simply won't
work), you should be wary if you are using the Microsoft FrontPage Extensions. The FrontPage
extensions use the .htaccess file so you should not really edit it to add your own information. If
you do want to (this is not recommended, but possible) you should download the .htaccess file
from your server first (if it exists) and then add your code to the beginning.
Custom Error Pages
The first use of the .htaccess file which I will cover is custom error pages. These will allow you to
have your own, personal error pages (for example when a file is not found) instead of using your
host's error pages or having no page. This will make your site seem much more professional in
the unlikely event of an error. It will also allow you to create scripts to notify you if there is an error
(for example I use a PHP script on Free Webmaster Help to automatically e-mail me when a page
is not found).
You can use custom error pages for any error as long as you know its number (like 404 for page
not found) by adding the following to your .htaccess file:
If the file is not in the root directory of your site, you just need to put the path to it:
Then, all you need to do is to create a file to display when the error happens and upload it and
the .htaccess file.
Htacess Commands
Introduction
In the last part I introduced you to .htaccess and some of its useful features. In this part I will
show you how to use the .htaccess file to implement some of these.
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
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.
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:
but this will still allow scripts to use the files in the directory.
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 exisit it will display a directory listing (unless, of course, you have
turned this off).
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 I find very
useful) is to redirect to a longer URL, for example in my newsletters I can use a very short URL
for my affiliate links. The following can be done to redirect a specific file:
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 directoires 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:
Then, any request to your site below /olddirectory will bee 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
http://www.newsite.com/newdirectory/oldfiles/images/image.gif
Password Protection
Introduction
Although there are many uses of the .htaccess file, by far the most popular, and probably most
useful, is being able to relaibly password protect directories on websites. Although JavaScript etc.
can also be used to do this, only .htaccess has total security (as someone must know the
password to get into the directory, there are no 'back doors')
There are a few parts of this which you will need to change for your site. You should replace
"Section Name" with the name of the part of the site you are protecting e.g. "Members Area".
The /full/parth/to/.htpasswd should be changed to reflect the full server path to the .htpasswd file
(more on this later). If you do not know what the full path to your webspace is, contact your
system administrator for details.
username:password
where the password is the encrypted format of the password. To encrypt the password you will
either need to use one of the premade scripts available on the web or write your own. There is a
good username/password service at the KxS site which will allow you to enter the user name and
password and will output it in the correct format.
For multiple users, just add extra lines to your .htpasswd file in the same format as the first. There
are even scripts available for free which will manage the .htpasswd file and will allow automatic
adding/removing of users etc.
http://username:[email protected]/directory/
Summary
.htaccess is one of the most useful files a webmaster can use. There are a wide variety of
different uses for it which can save time and increase security on your website.