HOME
stuff you never thought you wanted to know.

 

 
Configuring Apache webserver with .htaccess file

Apache .htaccess file - short tutorial and the most notable and useful htaccess examples.

The Apache Web server provides a feature called .htaccess file, which provides commands to control a Web site. An .htaccess file is simply a text file containing Apache directives. Those directives apply to the documents in the directory where the .htaccess 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 Web site 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 .htaccess 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 it's proper name .htaccess

Some sites do not allow use of .htaccess files, since they can slow down a server overloaded with domains if they're all using .htaccess files and some things that .htaccess 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 404 /404.html
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...
AddType text/html .shtml
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...

AddHandler server-parsed .shtml .html

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...

DirectoryIndex index.shtml index.html

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...
Redirect /OldDir/old.html http://site.com/NewDir/new.html

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...

Redirect /link http://www.MerchantDomain.com/affil.cgi?12345

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...

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?site.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ - [F]

Replace "site.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...

RewriteRule \.(gif|jpg)$ http://www.site.com/stop.gif [R,L]

Replace "site.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...
IndexIgnore *

The * matches all files. If, for example, you want to prevent only listing of images, use...

IndexIgnore *.gif *.jpg
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...
Apache User's Guide
List of Apache Directives you can use for your .htaccess files



Linux
Setup Software Raid 1 with LVM
Setup Linux with Desktop

Google
Manage your website ads with DFP
Google AdSense for Domains - not so great
Let Google Handle Email for your Domain Name
Page Rank banned by Google
Google's highest ranked web pages
SEO intelligent spam causes irrelevant search results
Google Sandbox
Google ranking factors
How to not give out page rank in web page links

Amazon
Amazon Cloudfront versus Shared Hosting

Web Server Programming
Simple Java web server
Simple Python web server
Configuring Apache webserver with .htaccess file

Windows
Turn off the loginscreen in XP, after installing .NET .

Turn off xp login screen unread mail count
What is .NET

Web (webmastering)
Introduction to Cascading style sheets (CSS)
The value of Alexa traffic rank
HTML META tag, not a search engine optimization tool
Create a maintainable webpage with modularization
The www prefix in your domain name
What is RSS and cool things RSS can be used for
MySql backup and restore with phpMyAdmin

Mix Computer related text
Doing business (making money) with Information Technology
Business with Computer Science
Research in Computer Science
Current and future possibilities of Medical Informatics
Tasks that make sense to have automated
Programming handheld mobile devices (overview)
Security tips for the web surfer
Price and Capacity in computer hardware
Java RMI Tutorial.

Microsoft Word
Page numbering in Word
Numbering headers or outlines in Word
Create a List of Figures
Turn off the default collapsing menus in Word




Turtlmeat.com 2004-2011 ©