.htaccess files can be used for access control, error redirection and more. To get you started with a real world example, let's look at server errors, custom error pages, redirection and htaccess:
Error Redirects
Some webpage requests result in server errors. These errors usually result in the display of a stock server-error page – something that nearly every surfer has encountered, probably more often than they would like. Different websites need to handle errors – and the pages they display in response to them – in different ways. They also should account for the reasons that errors are being generated and consider how best to deal with the situation.
For example, the most frequently encountered error page is the "404" or "Page not found" error. 404s typically result from syntax errors in the webpage's source code (meaning the user either misspelled the file name or used an incorrect path to point to it), or because web pages point to renamed, replaced or removed pages and files. Such errors typically are the fault of the webmaster, who either did not physically check (by manually clicking) every link on his or her website or failed to update website links to reflect changes in the linked files. 404s also are generated by curious surfers and "monkey boys" guessing their way around your free gallery files looking for the next TGP gallery in a series, for example, or even by folks trying to "guess" their way into your members area.
Regardless of the reason why a 404 is generated, dealing with the event by rerouting the traffic or displaying a custom error page, rather than ignoring it by simply displaying the stock server page, should be of prime concern to website owners – and the type of website should determine the type of response. Some operators sell this traffic outright or send it to sponsors – a potentially wise move, especially for traffic-pump and free-site operators. For these webmasters, 404 traffic is often seen as "garbage" traffic and disposed of quickly.
Pay site operators, on the other hand, should realize that a 404 often is generated when a surfer clicks a link and does not receive the page or file that he was expecting – something that may be the webmaster's fault. Rather than disappointing a paying customer, it's better to have a custom 404 page with links to your home page, site map and contact info. By helping this surfer, rather than sending him away or "ignoring" his or her needs, you build a better relationship, which may transform disappointment into longer retention rates.
Likewise, how you handle your "401" or "Authorization Required" errors can dramatically impact your profitability. 401s typically are generated by surfers after three failed attempts at entering a valid username and password combination while trying to gain access to a password-protected directory, such as /members/.
Once again, the type of website should determine the response. For example, a TGP, where 401s are being generated by "monkey boys" trying to hack into the admin system, should treat these errors differently than a paysite, which needs to ensure that legitimate customers have no difficulties in accessing content that they have paid for. In this case, a "help" page with customer service contact info should be displayed, offering an online form that surfers can fill out, allowing them to provide your customer service staff with the username / password combo that they were attempting to use.
There are other server error codes, some of which are not frequently encountered. You can find a list of codes and their meanings here. You can use this list to fine-tune your error traffic flow, but be careful that you're not causing problems for yourself or your website's visitors.
A Sample .htaccess File
While .htaccess files can have many specialized elements and perform tasks such as preventing bandwidth thieves from hotlinking your images and rewriting long, dynamic URLs into easily remembered short-form URLs, the "real world" example I want to show you contains only one line:
[CODE]ErrorDocument 404 http://www.domain.com/404-page.html[/CODE]
Copy the above line to a blank text document. Change the URL to point to your custom 404 error page, or to the destination you wish to send this traffic, such as your home page, hub, or sponsor, then save this page as ".htaccess" and upload it to your website's root directory. It's that simple...
You can add 401 redirection by adding an additional line:
[CODE]ErrorDocument 401 http://www.domain.com/401-page.html[/CODE]
[CODE]ErrorDocument 404 http://www.domain.com/404-page.html[/CODE]
You can also begin geo-targeting your traffic by adding a blank line under the code above, then pasting the redirect code from my previous article, "Real World Redirection" below it.
There's much more to the power of using .htaccess files; search our article archives for more examples and take control of your traffic today.