Friday, September 23, 2011

Understanding htaccess: Rewrite and Redirect

One of the tools available to system administrators that will work to make their job easier is their becoming conversant in .htaccess. This ability allows you greater control of your server's configuration and allows you to make changes without adjusting settings for the entire server. These changes are not only completed for the directories, but are picked up and used by subdirectories as well making your job easier and on the fast track to completion. Some favorite tools and tricks used by experts in system administration include redirects and rewrites.

Using Rewrites & Redirects

For redirects using optimal Search Engine Optimization (SEO) implementation, you first need to determine whether you want your redirects to point to the "www" or the "non-www" page of your website. It is crucial that whichever option you choose that you stay consistent.

This code below redirects non-www to www

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

You can use this code sequence to direct any traffic from one website, a blog for example, and redirect it to another site permanently. This can be installed using this code:

RewriteEngine on

RewriteRule .* http://www.newdomain.com/ [R=301,L]

Another similar tool that is used frequently for redirects is using a 301 redirect:

Redirect 301 /formersite.html http://www.yourdomain.com/bloghome.html
Redirect 301 /formersite2.html http://www.yourdomain.com/folder/Security

This code sequence changes a simple website page into a secure one with the use of SSL, assuming you have a dedicated SSL certificate available. Customers will be secure in the knowledge that their personal information is safe, especially on an e-commerce website. To implement this, use this coding:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Setting Website Access With .htaccess

Concerns about website security cannot be overlooked in today's technological world. Using .htaccess easily controls who has access to any part of your website. This includes development, testing and administrative access. To accomplish this, initially it is required that a file is set up to install a username and password. To build this file visit (http://tools.dynamicdrive.com/password/) and use the following coding to set a password protection for a specific directory on your website:

AuthUserFile /home/dave/.htpasswd
AuthName "My Personal Login"
AuthType Basic

require valid-user

Disallow Hotlinking For Optimal Security


One mistake that should never be overlooked is to allow hotlinking on a company website. Hotlinking allows copying of website content such as videos or images and enables embedding it into another website. This pulls on your bandwidth each time the website loads. This is easily avoided by preventing files from being hotlinked and stops the hijacking of bandwidth by thieves by using the following coding:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?domain.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpg|gif|bmp|png)$ /images/handsoff.jpg [L]

Error 404s And Custom Error Pages

Another major tool used frequently in the use of .htaccess are custom 404 error pages. This script sends visitors landing on a bad page with a redirect to a valid page. These pages typically include a sitemap and/or search bar rather than the dead end typically found on 404 error pages. By creating an error page that actually assists visitors, you will help avoid frustration and keep visitors returning. Building a custom error page can be done by using this code:

ErrorDocument 400 /errors/badrequest.html
ErrorDocument 401 /errors/authreqd.html
ErrorDocument 403 /errors/forbid.html
ErrorDocument 404 /errors/notfound.html
ErrorDocument 500 /errors/server.html

Learning to use .htaccess will give you the options you need to administer and control your website to provide great customer service and an efficient, easy to manage website.
By: Site Reference

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...