The Original Web-Wise-Wizard
Web Authors, Web Developers and Webmasters Internet Toolbox
Best viewed at 1024 x 768 using a colour depth greater than 256

Apache Mod Htaccess Tutorial

This page has been tested and conforms to WCAG 2.0 Accessibility Guidelines

Htaccess Files Howto...

Learn to harness the power of htaccess files to control what happens on your Website. This detailed tutorial contains a range of realtime examples that you can use for redirecting pages, stopping bandwidth theft, error handling, improving Google PageRank, plus many other useful tips and tricks.

Webmasters or site owners whose server administrator or Web Hosting service allows the use of htaccess files (access files) can achieve a professional level of site management and control of their website's configuration and behaviour by the careful use of this very powerful and efficient facility. Htaccess files are typically supported by Apache or other HTTPd compliant web servers.

Using Htaccess Files ...

Htaccess files are plain old fashioned ASCII text files that should be created in a standard text editor. You can locate them in the root directory of your web server, in which case and where applicable, all sub-directories inherit the properties from the root directory htaccess file, or they can be located in sub-directories, in which case the directives can override the directives in the root directory htaccess file. Dependant on the configuration of your web server, htaccess files can also override the configuration directives of the web server itself but only as they relate to your website. In other words an htaccess file is an extension of the web server's main configuration file/s.

You can only have one htaccess file per root directory or subdirectory and every directive must start on a new line. Comment lines must start with a HASH sign ('#') and you can use blank lines between directives or groups of directives. I personally find it useful to use upper case for comments and to use a blank line between groups of directives. In accordance with good programming practice everywhere, you should use comments to give a brief description about what each group of directives does or is intended to accomplish.

# THIS IS A COMMENT LINE
 
# THE LINE ABOVE IS BLANK
This would be treated as a directive because there is no hash sign at the beginning

Htaccess file syntax is based on directives used in HTTPd compliant web server configuration files, regular expressions, the Perl programming language and HTTP Protocol status codes. If all this means little to you then I strongly suggest that you limit yourself to using pre-written working examples, changing only those parts of a directive that specifically refer to your site or it's pages.

Htaccess Warning ...

If you upload an htaccess file to your web server using FTP and you find that your website has started to mis-behave or is not working correctly then FTP into the server and delete or rename the htaccess file to eliminate this as the cause of the problem. When I was new to htaccess files this did happen to me and it took me a long time to figure out that a mis-configured htaccess file was the cause of the problem. I was eventually forced to telephone Tech Support (how humiliating) and even they did not spot the cause of the problem. Their only solution was to delete the existing webside and create a new one with me having to upload all the files (including the mis-configured htaccess file) to get a fresh start. I really was in a bad mood and was calling them for everything.

Permanent Redirect 301 ...

# PERMANENT REDIRECTS
RedirectPermanent /home/home.shtml http://www.web-wise-wizard.com/

Temporary Redirect 302 ...

# TEMPORARY REDIRECTS
RedirectTemp URL-path URL

Improve Site PageRank ...

Google sees www.example-domain.com and example-domain.com as different Website addresses. Because some external sites will link to the first version and other external sites will link to the second version, the number of backlinks to each version of the address will almost certainly be different. This is likely to result in each version of the address having a different Google PageRank and it also means that your site's PageRank will be split, often resulting in a lower overall PageRank for the site than you would otherwise receive. To consodilate the two versions of the address and regain any lost PageRank you can use an htaccess file to focus all the PageRank into one version of the address.

Your Web server must support htaccess files and it must also support Mod_rewrite. For some Websites with a large split in backlinks to each version of the address, this issue could be important enough to consider changing to a different Web Hosting company if your existing Web server does not support these features.

Place the following directives into an ordinary text file called '.htaccess' (don't forget the DOT in front of 'htaccess') and then upload the file to the root directory of your Web server. If you already have an htaccess file in your root directory then simply add the following three directives to the end of the file, making sure that you start on a new line.

do not forget to substitute your domain name for web-wise-wizard.com ...

RewriteEngine On
RewriteCond %{HTTP_HOST} ^web-wise-wizard\.com
RewriteRule ^(.*)$ http://www.web-wise-wizard.com/$1 [R=permanent,L]

These directives intercept all HTTP requests to the site that do not have a 'www.' at the beginning of the Domain name and then force a redirect to the same page address only this time, including the 'www.' in the Domain name. It may take Google some time to sort itself out but using these directives should eventually lead to the consolidation of your Websites PageRank.

Default Character Set ...

We hosted this site on an Apache web server that sent a UTF-8 character set directive in the header of the web page. This UTF-8 directive overrode our page META directive which specified the ISO-8859-1 character set. The UTF-8 character set did not correctly display HTML Entities so we used the following directive to change the page headers to specify the ISO-8859-1 character set.

do not confuse the AddDefaultCharset directive with the DefaultCharset directive...

AddDefaultCharset ISO-8859-1

Server Parsed Pages ...

The first directive uses a MIME type to instruct the web server which type of files should be treated as HTML documents. HTM and HTML files would typically have been previously configured in this manner but this directive adds SHTML files to the list. The second directive instructs the web server to parse SHTML and HTML documents before serving the pages. These directives would typically be required to enable SSI (server-side includes) which enables you to include common or dynamic content in your web pages.

# ENABLE SERVER-SIDE INCLUDES
AddType text/html .shtml .htm .html
AddHandler server-parsed .shtml .html

Forbid Bad Spiders ...

These directives can be used as a second line of defense against those unwanted spiders that ignore requests in the robots.txt file. The only problem is that the most unscrupulous spiders will keep changing their HTTP_USER_AGENT string which means that you are continually having to play 'catch up' with them and the size of your HTACCESS file can start to grow. These directives require the Apache Mod Rewrite module to be enabled on the web server.

# REDIRECT BAD SPIDERS
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^Scooter*
RewriteRule ^.*$ http://www.web-wise-wizard.com/http-403-forbidden.html [L,R]

Directory Index Page ...

The browser displays a directory listing of all the files in a directory on the web server. Create an HTACCESS file with one of the following directives and then upload it to the directory in question.

DirectoryIndex index.php index.html index.shtml index.htm
DirectoryIndex index.htm index.html index.shtml /cgi-bin/dynamic-index.cgi

Server Path Alias ...

An alias is often used with a cgi-bin or other sensitive directory for added security. For example, the real cgi-bin directory may be located above the root directory of the web server so that it cannot be accessed from the web and an alias is used so that files in the real cgi-bin directory can be addressed in URLs. In the example below we have created a '/styles/' alias to address a directory well away from the web server area of the web server's hard drive.

Alias /styles/ /usr/wizard/special-styles/
 
 

Link Directly To This Page ...

help support free information on the Internet ...

Many users prefer to link directly to individual content pages on Web-Wise-Wizard. If you would like to do this then we have provided the following HTML/CSS link script which you can copy and paste directly into your HTML editor. Alternatively, you might like to use our New Dynamic Link Generator to create a link that more fully meets your own particular requirements.

the link displayed ...

Web-Wise-Wizard - Apache Mod Htaccess Howto Harness the power of htaccess files to manage your Website. This tutorial includes examples to redirect pages, stop bandwidth theft, error handling, improve PageRank, plus many other tips and tricks.

select/copy the link Markup ...

Featured Tutorial
Want More Traffic? Increase your Link Popularity and your PageRank by learning how to use Web Directories
Link To Us Scripts
New Dynamic Link Generator
 
If you find this page interesting or useful then others are likely to view it in exactly the same way. Providing a link to the page will be considered by the search engines as casting a vote for the page. In turn, this will help to improve the search engine ranking of the page resulting in more people being able to see the page. Your link really does count so please don't delay.
 
Post your link NOW!
 
 
Useful Apache HTACCESS Websites
 
 
 
 
Copyright © 1998,2014, Gilbert Hadley, Liverpool, England