Return to CGI Library
.htaccess files
This strangely named file is key to getting the most out of your virtual server. You can use it to:
- Redirect hits from one URL to another.
(Useful when you've deleted or moved a page or a whole site.).
- Turn on/off password protection.
- Allow / Deny access to specific visitors.
- Set the Error messages for various operations
(e.g.Produce a more more friendly page than "404 Object Not Found".)
- Set or change a Mime-type if the system file doesn't know about the mime-type you want to use.
Other (not documented here) capabilities include:
- Turn on/off some of the server features -- such as automatic directory indexing.
- Turn on ident checking or turn off DNS lookups (for speed).
These aren't new features, but some of them are only recently controllable from the .htaccess file. The definite source of information
is the Apache web site. We've tried to extract and summarize some of the relevant
information here.
Introduction
The ".htaccess" file is just that. A file called .htaccess located in one or many of the directories in your virtual server.
Traditionally this file has controlled access to the pages in a directory (thus the access part of the file name -- and this is what
you get if you search the web for .htaccess). With the newer versions of Apache, some of the server configuration parameters are
controllable from the this file.
When your virtual server goes to hand out a file, it starts checking in the top directory for a .htaccess file, and then checks each
subdirectory down to and including the directory that your file is in. Thus you can set the defaults for a virtual server by placing a
.htaccess file in the top directory of that virtual server, or for multiple virtual servers at once by placing the file in your home
directory (e.g. one directory above the top directories of all your virtual servers).
This is a nice way to push a browser over to a different page if you've
re-organized your web-site :-). If you moved a page in somedir from
page1.htm to page2.htm then the appropriate line to add to
your .htaccess file would be:
Redirect PERMANENT /somedir/page1.htm http://your.domain/somedir/page2.htm
Since you get to specify the full URL to the new page, you can bounce the browser to another server if you want. This also works for directories too :-).
Adding the permanent flag should be more search engine friendly for
real site changes (it's a 301 status code). If you just want a temporary
redirect you can use "Redirect TEMP". Also supported are "Redirect SeeOther"
and "Redirect Gone". SeeOther indicates that the page has been replaced
Gone indicates there is no replacement (and you don't need to specify
a target URL for gone). (The directives are not case sensitive,
REDIRECT temp will work. The target URLS probably are case sensitive.)
Access Control
Boring. We've already documented the Password gadget, which contains links to
documents describing access control by IP address or domain name. Search the web... that's all been said before :-).
Error Documents
Sigh... I suspect we've all seen this before:
File Not found
The requested URL /someone/mistyped/a/link.html was not found on this server.
|
We have also, probably run into a page like the following:
|
Oops:
|
Oops! You've found a bad link!
Please consider starting from our top page and working down to the page you were looking for.
|
Often these pages have a link back to the referering page, and might show you the URL that you tried to access. That is easily
accomplished with a little CGI, PHP, or even creative use of SSI's, but that's beyond the scope of this little resource page.
These pages are examples of Error Documents. The top table is the default, and the bottom table is an example of what you can replace
it with. You can even have one for each directory if you want...
(linking to /gadgets/ instead of / for example.)
Most types of errors that the web server can run into have error numbers. For example "Not Found" is error 404.
( List of Errors below.) The specification of an error document is easy. You add "ErrorDocument", the three
digits of the error number and the either the error string or the page to go to. The following examples show the three forms:
ErrorDocument 401 "<body bgcolor="#ffffff"><h1>You have to <b>BUY</b> a
<a href="/www/">subscription!</a>
ErrorDocument 403 /gadgets/htaccess/forbidden.html
ErrorDocument 404 /gadgets/htaccess/notfound.html
or (the less preferred form:)
ErrorDocument 404 http://baremetal.com/gadgets/htaccess/notfound.html
Error in Client |
Number | Description |
400 | Bad Syntax |
401 | Unauthorized |
402 | Not Used (Payment Granted) |
403 | Forbidden |
404 | Not Found |
Error in Server |
500 | Internal Error |
501 | Not Implemented |
502 | Overloaded |
503 | Gateway Timeout |
Changing MIME-Types
Changing Mime-types is easy. Just create a .htaccess file that looks like this:
AddType new-mime-type extension
Here's an example and the here's the
.htaccess file used. Please note that the period is NOT
included in the extension.
|