How-to Guide: Redirects and htaccess

By Bret Fencl, Fencl Web Design



Every time you turn around another article on the Internet tells you to make a 301 redirect for the obsolete pages on your website. The problem is that most of these articles do not explain how to make a proper redirect.

First Check for a .htaccess File
Check to see if you have a .htaccess file in the main directory of your website. The base directory for a website is usually called something like htdocs, httpdocs, www or public, and is the file where your home index page resides.
 
On many servers the .htaccess file may be hidden from FTP software, so you will want to try accessing it through your Web hosting file manager. If you still cannot find a .htaccess file, then check with your Web hosting company to make sure you do not overwrite it, which could damage your Web hosting or site.
 
If you find the .htaccess file, make sure you save a backup copy before making any changes to it.

Creating a .htaccess File
If you do not have a .htaccess file you will need to create one.

This is a basic text file and does not have any extension on the end of it. Simply create a new text file and name it .htaccess and save the file starting with a period(.).

Once you upload this file to your server it may become hidden or disappear. But don't worry, it will still function properly.

Creating a 301 Redirect
A 301 redirect is used to send a visitor that was trying to access an outdated page of your website to the new page. This also assists many search engine bots in finding the new page and dropping the old page from the index.

The command you would use in your .htaccess file is simply:
redirect 301 oldpage.html newpage.html

You may also use the same method to redirect visitors from an old file to a new page.
redirect 301 /oldfile/ newpage.html

Creating a 302 Redirect
A 302 redirect is used much the same as a 301 redirect but the difference is that it tells the search engine bots that this move is only temporary and the old page will be returning soon. This is particularly useful for retailers who may have run out of a product and will have it back in stock in a week so you temporarily point the page to a similar product to avoid losing sales.

For a 302 redirect, simply enter this code into your .htaccess file:
redirect 302 oldpage.html temporarypage.html

Creating a Mod Rewrite
Sometimes you can simply make a redirect by rewriting the name of a new page in the same file directory of a website without actually changing the page name your visitor's access. You need to check with your Web host to see if they allow mod rewrite - many Web hosting companies do.

For example, you have a product that you want to advertise on your website and send to your customers in an email newsletter. A long web address may break when you email a URL such as: https://www.example.com/index.php?cart=98234&id=4593921

Another need for this situation is that it may make it easier to explain the Web address over the phone or write it down for someone on the fly.

The above URL could be shortened to:
https://www.example.com/widget.php

Here is the .htaccess command for a Mod Rewrite on a URL:
Options +FollowSymLinks
RewriteEngine on
RewriteRule widget.php index.php?cart=98234&id=4593921


Redirects are important for websites to remain up-to-date and for the search spiders to consistently index fresh content. While this is simply a primer to .htaccess and redirects, and you may have to try a little experimenting, soon you will learn fun new uses for redirects and mod rewrite and make sure that your site never appears outdated.

 

Block Bad Bots

Some bots will ingnore directions available in the robtoots.txt files but they can be blocked using a .htaccess file. One of the downsides of this method is that you can't actually block bots by their actual name, but you can block them by matching the beginning of the User-Agent string. 

SetEnvIfNoCase User-Agent "^EmailSiphon" bad_bot
SetEnvIfNoCase User-Agent "^EmailWolf" bad_bot
SetEnvIfNoCase User-Agent "^ExtractorPro" bad_bot
SetEnvIfNoCase User-Agent "^CherryPicker" bad_bot
SetEnvIfNoCase User-Agent "^NICErsPRO" bad_bot
SetEnvIfNoCase User-Agent "^Teleport" bad_bot
SetEnvIfNoCase User-Agent "^EmailCollector" bad_bot

 
Order Allow,Deny 
Allow from all
Deny from env=bad_bot


 
Website Magazine