On this page ...
Some webhosts (and some Internet
Providers) allow you to either use Frontpage Extensions or a so
called .htaccess file.
I have never used the Frontpage Extensions,
since I really hate Frontpage as an editor for my webpages, instead
I use Dreamweaver. So no Frontpage Extensions for me. Instead I
use the so called .htaccess file.
So what can we do with the .htaccess
file?
Well, we can create custom error
pages, automatically redirect obsolete links, regulate access, etc.
Note: Not all providers
offer the use of the .htaccess file, please contact your provider
and ask him if this is supported.
Warning: Frontpage
is not handling the .htaccess files well!
Note: You can define
an .htaccess file for each folder, over-ruling the root .htaccess
file!
Note: Some dedicated servers
expect you to use absolute paths, so instead of /bla.htm
you should use http://www.yoursite.com/bla.htm.
|
|
.htaccess file - Overview
This strangely named file is they key to getting the
most out of your virtual server. Actually, the filename makes pretty good
sense once you know that files that start with a dot, are hidden files
in a Unix-like environment, and that 'ht' is derived from 'HTTP',
and well access from access.
The .htaccess file is basically a text file with
some instructions, stored somewhere on the server (preferably in the root
of your website).
Deny access to certain IP-addresses
You can deny access to certain IPs. If you don't want
someone to access your site place the following code into your .htaccess
file:
<Limit GET>
order allow,deny
deny from 123.456.789.0
deny from 123.45.67
allow from all
</Limit>
This code will block 123.456.789.0 IP and all others
that are starting with 128.45.67. Everybody else will be able to access
your site. You can block as many addresses as you want.
Define my default webpage
Most servers have index.html as their default page.
This means that when you access http://www.weethet.nl/, you will automatically
be redirected to http://www.weethet.nl/index.html.
Let's say you want the default to be start.html.
All you need to do is to add the following line to your .htaccess
file:
DirectoryIndex start.html
Automatically redirecting
users to another page
It may be necessary to redirect a user from one page
to another.
Commonly this is needed when you have restuctured your
website and gave some files a new location, or - like I did - had to rename
all files because I started using ASP.
Without a redirect, the user will encounter a 404
File Not Found Error from you site. This is not very usefull to the
visitor, you'd rather redirect him or her to the new location or new file
that replaces the old one, so the visitor can see the info they are looking
for.
File
to File redirection
You could go for a little JavaScript (see Autoredirect
in HTML) or rather simple by adding a line to your .htaccess file:
Redirect /oldpage.htm /newdirectory/newpage.php
You can add as many lines as necessary, just keep it
in the same format. You can also redirect several pages to one main page.
I used that for showing the error, informing the user to update their
bookmakrs and to automatically redirect to the actual page. Click
here to see a life example.
Directory
to Directory redirection
Suppose you used to have a directory /dutch/content,
but you moved the files to /blabla/textfiles.
This means that each URL with "/dutch/content/" should be replaced with
the same lnk with "/blabla/textfiles/" instead:
Redirect /dutch/content/ http://www.weethet.nl/blabla/textfiles/
Iedere link waarin dus staat http://www.weethet.nl/dutch/content/,
wordt veranderd naar http//www.weethet.nl/blabla/textfiles/
Als voorbeeld;
je wilt de pagina http://www.weethet.nl/dutch/content/search.php openen.
De redirect maakt daar dan van http://www.weethet.nl/blabla/textfiles/search.php.
Filetype
to filetype redirection
This one looks a bit more complicated. Say you want
to redirect all ASP pages
(*.ASP)
to PHP pages (*.PHP).
RedirectMatch (.*)\.asp$ http://www.weethet.nl$1.php
Here each *.asp link will be redirected to the
same link, now with the .php extension. Basically it says:
zero or more random characters (="(.*)\")
ending with .asp (=".asp") at the end of
the line (="$"),
should be redirected to http://www.weethet.nl (="http://www.weethet.nl")
with the same link as the found string minus the .asp extension (="$1")
with a .php extension (=".php").
Preventing someone from viewing
my .htaccess file
The .htaccess file typically resides in your
root directory and can be viewed through the web browser. Most of the
time there is information contained the the .htaccess file that
you don't want people knowing, like the rules for allowing or denying
access. One way to prevent access to the .htaccess file is to disable
access to that particular filename. You can add the following lines to
your .htaccess file in the root directory to deny visitors from
viewing all .htaccess files contained in your website:
<Files .htaccess>
order allow,deny
deny from all
</Files>
Prevent someone from linking
to my downloads
This may sound a bit sick and sad, but now-a-days, one
get's billed for the bandwidth used, so if others do not wish to have
these costs (or they are to lazy) they might link to the files on your
site rather than linking to your website in general.
This text refers to an .htaccess file inside
your download directory!
Add these lines to your .htaccess file and stop
people from linking to your downloads and claiming it is theirs. You can
also redirect them to another page of your choice.
AuthUserFile /dev/null
AuthGroupFile /dev/null
RewriteEngine On
RewriteCond %{HTTP_REFERER} !>http://www.example1.com [NC]
RewriteCond %{HTTP_REFERER} !>http://example1.com [NC]
RewriteCond %{HTTP_REFERER} !>http://www.example2.com [NC]
RewriteCond %{HTTP_REFERER} !>http://www.example3.com [NC]
RewriteCond %{HTTP_REFERER} !>http://123.123.123.123 [NC]
RewriteRule /* http://www.example.com/index.html [R,L]
"http://www.example1.com"
(example2, etc.) is the URL where the downloads can occur.
The numbers 123.123.123.123 is your IP
address for your site.
"http://www.domain.com/page.htm" is the page
that an unauthorized person is redirected to. Be sure to keep the syntax
in place and only change the URL's to your domain.
This will stop the download of any files in it's directory.
You should create a separate directory for your downloads. If you place
this file in the root directory you will be redirecting them to your RewriteRule
page.
Custom error pages
I suspect we've all seen this before:
404 - File Not found
The requested URL /someone/mistyped/a/link.html was not found on
this server.
|
But we've probably all run into a page like the following
too:
Oops! You've found a bad link!
You will be redirected automatically
to our main page.
|
These pages are examples of Error Documents.
The top example is the standard error (404 - File Not Found), and the
bottom example is an example of what you can replace it with. You can
even have one for each directory if you want. Often these pages have a
link back to the referering page or a link to the new location of an old
page.
Most types of errors that the web server can run into
have error numbers. For example "File 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 404 http://www.domain.com/404.html
ErrorDocument 401 /401.html
ErrorDocument 500 errors.php?error=500.html
Error List
Number |
Description |
400
|
Bad Syntax |
401
|
Unauthorized |
402
|
Not Used |
403
|
Forbidden |
404
|
File Not Found |
|
|
Number |
Description |
500
|
Internal Server Error |
501
|
Not Implemented |
502
|
Server Overloaded |
503
|
Gateway Timeout |
Avoid directory browsing
Som websites allow you (this is a defaut setting!)
to browse directories on your webserver by simply entering URL+directory
name in a webbrowser. Usually this is not something you would want. Copy
the following line into the .htaccess file found in the root of the website,
it will disable this browsing feature.
Options Includes FollowSymLinks
|