Apache Redirects for SEOs

Definitions and Glossary

Overview

What you are trying to accomplish here is to have one resource (either a page or an entire site) redirect a visitor to a completely different page or site, and while doing so tell the visitor’s browser that the redirect is either permanent (301) or temporary (302).

Therefore you need to do three things:

  1. Have 2 resources – one source page or website, and one destination page or website.
  2. When an attempt to  access the source resource is made, the webserver transfers the visitor to the destination instead.
  3. During the transfer, the webserver reports to the visitor that a redirect is happening and it’s either temporary or permanent.

The ability to control the “status” argument in the redirect directive (which sets whether it’s a 301 or 302) within Apache is only available in version 1.2 and above. You are best off using version 2 or above for maximum stability, security and usefulness.

Mod_Rewrite and the Apache Redirect

If you have the mod_rewrite extension installed (it comes with most Apache installs as a default) you can use it to dynamically change URL’s using arguments on the fly – this is NOT a 301 redirect, but rather it’s related behavior. For example, if you wanted to redirect .htmfiles from an old server to their equivalent .php files on a new one using a 301 redirect, you would use a combination of mod_rewrite and the redirect directive to do the redirection + URL change.

You could do it on a file by file basis by making a really long list of possible redirects in the .htaccess file by hand without mod_rewrite, but that would be a real pain on a server with a lot of files, or a completely dynamic system. Therefore these 2 functions are often used together.

Syntax for a 301 Redirect

The syntax for the redirect directive is:

Redirect /yourdirectory http://www.newdomain.com/newdirectory

If the client requests http://myserver/service/foo.txt, it will be told to access http://www.yourdomain.com/service/foo.txt instead.

Note: Redirect directives take precedence over Alias and ScriptAlias directives, irrespective of their ordering in the configuration file. Also, URL-path must be a fully qualified URL, not a relative path, even when used with .htaccess files or inside of <Directory> sections.

If you use the redirect without the status argument, it will return a status code of 302 by default. This default behaviour has given me problems over the years as an SEO, so it’s important to remember to use it, like this:

Redirect permanent /one http://www.newdomain.com/two

or

Redirect 301 /two http://www.newdomain.com/other

Both of which will return the 301 status code. If you wanted to return a 302 you could either not specify anything, or use “302” or “temp” as the status argument above.

You can also use 2 other directives – RedirectPermanent URL-path URL (returns a 301 and works the same as Redirect permanent /URL PathURL) and RedirectTemp URL-path URL (same, but for a 302 status).

For more global changes, you would use redirectMatch, with the same syntax:

RedirectMatch 301 ^(.*)$ http://www.newdomain.com

or

RedirectMatch permanent ^(.*)$ http://www.newdomain.com

These arguments will match any file requested at the old account, change the domain, and redirect it to the file of the same name at the new account.

You would use these directives in either the .htaccess file or the httpd file. It’s most common to do it in the .htaccess file because it’s the easiest and doesn’t require a restart, but the httpd method has less overhead and works fine, as well.

Simple Domain 301 Redirect Checklist

This assumes you just have a new domain (with no working pages under it) and want it to redirect properly to your main domain.

1. Ensure that you have 2 accounts – the old site and the new site (they do not have to be on different IP’s or different machines).

2. Your main (proper or canonical) site should be pointed at the new site using DNS. All your other domains should be pointed at the old site using DNS. Parking them there is fine at this point.

3. Find the .htaccess file at the root of your old account. Yes, it starts with a “.” We will be working with this file. The new site does not need any changes made to it – the old site does all the redirection work.

4. Download the .htaccess file and open it in a text only editor.

5a. Add this code:

Redirect 301 / http://www.newdomain.com/

6. Then upload the file to your root folder and test your new redirect. Make you you also check it using a HTTP Header viewer just to be sure it shows as a 301.

Control Panel Method

cPanel redirect

  • Log into your cPanel, and look for “Redirects” under Site Management
  • Put in the current directory into the first box
  • Put the new directory in the second box
  • Choose the type (temporary or permanent) temporary=302 and permanent=301
  • Click “Add” and you’re done

You can only do 302 redirects (or frame forwarding – bad!) using the Plesk control panel – use .htaccess for 301’s instead.

If you use Ensim, the only way to redirect is by using the .htaccess file (no control panel option at this time).

Basic Old Website to New Website Redirection

This is used when you have an existing website (with pages) and want to move it to a new domain, while keeping all your page names and the links to them.

1. Ensure that you have 2 websites – the old site and the new site, and that they are on different accounts (they do not have to be on different IP’s or different machines).

2. Your main (proper or canonical) site should be pointed at the new site using DNS. All your old domains should be pointed at the old site using DNS.

3. Find the .htaccess file at the root of your old account. Yes, it starts with a “.”  We will be working with this file. The new site does not need any changes made to it – the old site does all the redirection work.

4. Download the .htaccess file and open it in a text only editor.

5a. If you have mod_rewrite installed, add this code:

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

5b. If you don’t have mod_rewrite installed, you really should. If you can’t install it, then you can use this code instead:

RedirectMatch 301 ^(.*)$ http://www.newdomain.com

6. Then upload the file to your root folder and test your new redirect. Make you you also check it using a HTTP Header viewer just to be sure it shows as a 301.


FrontPage on Apache

After you’ve done the basic Apache 301 redirection described in this article, you will also need to change the .htaccess files in:  

_vti_bin
_vti_bin /_vti_adm
_vti_bin/ _vti_aut

Replace “Options None” to “Options +FollowSymLinks”

Those folders are part of your FrontPage extensions on the server, so you will have to use FTP to get to them, since FrontPage hides these folders by default to prevent them from accidentally being messed with by novice users.

More Complicated Redirects

You can’t use a control panel in Apache currently for these – .htaccess only.

Redirecting everything to a single page

This is common when you are totally changing the new website from the old and you just want all your links and requests form the old site to be directed to a spot on your new site (usually the home page). You actually need to do it on a page by page basis.

Redirect 301 /oldfile1.htm http://www.newdomain.com
Redirect 301 /oldfile2.htm http://www.newdomain.com
Redirect 301 /oldfile3.htm http://www.newdomain.com

Redirection while changing the filename

This example will redirect all the files on the old account that end in html to the same file on the new account, but with a php extension. You can also use this technique within the same account if you want to change all your extensions but don’t want to lose your incoming links to the old pages. This is common when people switch to from static htm files to dynamic ones while keeping the same domain name, for example.

Just change the “html” and “php” parts of the below example to your specific situation, if needed.

RedirectMatch 301 (.*)\.html$ http://www.newdomain.com$1.php

Redirection while changing the filename, but keeping the GET arguments

Sometimes, you will want to change to a different CMS, but keep your database the same, or you want to switch everything but you like the arguments and don’t want to change them.

RedirectMatch 301 /oldcart.php(.*) http://www.newdomain.com/newcart.php$1

This will result in “http://www.olddomain.com/oldcart.php?Cat_ID=Blue” being redirected to “http://www.newdomain.com/newcart.php?Cat_ID=Blue”

URL Forwarding

Many ISP’s and domain registrars offer a service called URL Forwarding, or something similar. This term has no proper meaning and can mean pretty much anything that the registrar’s marketing team or tech’s decide it should mean or do. Most commonly, it’s a 302 redirect. Sometimes it’s a CNAME, park or even a frame. Always check to see what type of “forwarding” is actually done.

If they don’t know, consider switching to a company with more technical skill, or asking to see a demonstration URL and then checking it with an Http Header viewer.

CNAME Record

There is one other type of redirect, and that is by using the CNAME function, which is actually an alias for one domain to another. A common example is when someone uses CNAME to point www.mcanerin.com to just plain mcanerin.com. This apparently works just fine, and it technically tells the visitor that mcanerin.com is the main (canonical) domain and that www version is an alias.

In Apache, the header response for this is a 301. So yes, a CNAME can be used as a “poor man’s” 301 redirect. There are 2 serious problems, though. First, you can’t CNAME the “origin point” – the actual domain name itself – only sub domains, like www, ftp, mail, etc.

The second problem is that because it’s really easy to mess up CNAMES by accidentally setting up an infinite loop, they are not really recommended unless you know exactly what you are doing. For example, using a CNAME record will often break your email unless you are really careful and know exactly what you are doing. In general, avoid using CNAME.

The only time it’s really useful is when you want to alias a sub domain under the current domain to an external domain.

Conclusion

This type of redirection works great if you are using Apache and have access to your .htaccess file. If you don’t (and don’t want to switch hosts) then you will have to use redirection scripting to accomplish this. You’ll learn more about that in the Redirects Using On-Page Scripting and Headers section.

Next: IIS Redirects – 301, 302

Definitions and Glossary


Main Article

Detailed Technical Information

Specific Scenarios and How To Deal With Them

Unless otherwise noted, all articles written by Ian McAnerin, BASc, LLB. Copyright © 2002-2007 All Rights Reserved. Permission must be specifically granted in writing for use or reprinting anywhere but on this site, but we do allow it and don’t charge for it, other than a backlink. Contact Us for more information.