The Apache Web server provides a feature called .htaccess file, which provides commands to control a website. This file is simply a text file containing Apache directives. Those directives apply to the documents in the directory where the file is located, and to all subdirectories under it as well. Other .htaccess files in subdirectories may change or nullify the effects of those in parent directories.
You have to be careful when editing .htaccess files, as a small mistake can make your website stop working. You should immediately test the site to be sure it works.
You can use any text editor to create or make changes to .htaccess files. Keep in mind that commands in these files should be placed on one line only, so if your text editor uses word-wrap, make sure it's disabled. Be sure .htaccess file is uploaded in ASCII mode, not BINARY, or it won't work.
Your text editor or operating system may probably not allow to save file as .htaccess. The solution is to save the file as htaccess.txt and upload it to your server. After doing that, you should use your FTP client and rename the file to its proper name.
Some sites do not allow use of .htaccess files, since they can slow down a server overloaded with domains if they're all using such files, and some things that they can do can compromise a server configuration that has been specifically setup by the admin. Just be sure to read their TOS carefully or ask permission from your host.
Here are the most notable and useful .htaccess examples...
Custom error pages
The most common errors are 404 (Not Found) and 500 (Internal Server Error). Design your custom Web pages for these errors (you aren't limited to these errors, you can create an error page for each and every error). Add the following commands to your .htaccess file...
ErrorDocument 500 /500.html
You can name the pages anything you want, and you can place them anywhere you want within your site. The initial slash in the directory location represents the root directory of your site.
Enabling SSI
If you want to use SSI, but can't do so with your current Web host, you can change that with .htaccess file. The following lines tell the server that any file named .shtml should be parsed for server side commands...
AddHandler server-parsed .shtml
Options Indexes FollowSymLinks Includes
If you don't care about the performance hit of having all .html files parsed for SSI, change the second line to...
If you're going to keep SSI pages with the extension of .shtml, and you want to use SSI on your index pages, you need to add the following line to your .htaccess file...
This allows a page named index.shtml to be your default page, and if that isn't found, index.html is loaded.
Redirects
You can use .htaccess file to redirect any request for a specific page to a new page...
Server-side redirects are very useful for shortening affiliate links.
Your visitors won't be turned off by long links that are obviously affiliate links.
For example, to create a redirect at the URL:
http://YourSite.com/link
to point to the URL:
http://www.MerchantDomain.com/affil.cgi?12345
put this line in your .htaccess file...
Protecting your bandwidth
"Bandwidth stealing," also known as "hot linking," is linking directly to non-html objects on another server, such as images, electronic books etc. The most common practice of hot linking pertains to another site's images.
To disallow hot linking on your server, create the following .htaccess file and upload it to the folder that contains the images you wish to protect...
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?YourSite\.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ - [F]
Replace "YourSite.com" with your own. The above code causes a broken image to be displayed when it's hot linked. If you'd like to display an alternate image in place of the hot linked one, replace the last line with...
Replace "YourSite.com" and stop.gif with your real names.
Preventing directory listing
Typically servers are setup to prevent directory listing, but often they aren't. If you have a directory full of downloads or images that you don't want people to be able to browse through, add the following line to your .htaccess file...
The * matches all files. If, for example, you want to prevent only listing of images, use...
Redirecting YourSite.com to www.YourSite.com
If search engines find both www and non-www links from other sites to your site, they may treat http://YourSite.com and http://www.YourSite.com as two different websites with the same content. This means that your site can be penalized for duplicate content.
Many experts recommend to set up a 301 redirect (permanent redirect) from YourSite.com to www.YourSite.com...
RewriteCond %{HTTP_HOST} ^YourSite\.com [nc]
RewriteRule (.*) http://www.YourSite.com/$1 [R=301,L]
Replace "YourSite.com" with your real domain name.
The .htaccess file is very obscure and extremely useful when used properly. The above htaccess examples cover only a few possible uses of this powerful tool. For more information, see...
or (preferably) purchase a great book teaching all techniques needed to administer Apache on a Linux box...
Linux Apache Web Server Administration by Charles Aulds |
This is the most complete, most advanced guide to the Apache Web server you'll find anywhere. This book teaches you, step-by-step, all the standard and advanced techniques you need to know to administer Apache on a Linux box. Hundreds of clear, consistent examples illustrate these techniques in detail.
What's New
How to Create a Website
One-page guide for beginners.
Easy Website builders
Easy way to build a professional looking site for commercial use or just for fun.
Professional Web page templates
Remote computer access
New Web-based approach to remote computer access.