Graymatter Tech Tip: 301 Redirect in Apache

Apache Redirects Overview

You redirect an old or out dated page to another location. There are two ways of doing this:  a permanent command (301) or temporary command (302).

There is some information you need to get together:

 

  • Two resources - one old page or website, and a new page or website.
  • When someone goes to your old page they will be redirected by the server to go to the new page.
  • while this is happening the server tells the brower that this is a permanent or temporary state.
  • Controlling the "status" argument in the redirect directive (which sets whether it's a 301 or 302) in Apache is only available in version 1.2 and above, use 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 .htm files 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 /olddirectory http://www.newdomain.com/newdirectory

    If the client requests http://myserver/service/test.txt, it will be told to access http://www.yourdomain.com/service/test.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. More for a later post.



    Blogged with the Flock Browser

    Tags: , ,

    Permalink Print

    Filed under Blog by

    glogo.jpg

    Creative Commons License
    This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.