A 301 Redirect Guide for the Clueless!

Ranked #196 in Internet, #7,793 overall

A 301 Redirect Guide for the Clueless!

This quick tutorial will teach you how to perform an Apache htaccess 301 redirect in several different scenarios...no matter what your situation is!

Whether you want to delete or consolidate web pages, it's always best to pass all of their search engine merit over to another existing page, to salvage visits, page rank and old backlinks. This is what a 301 redirect will do for you!

Bookmark and Share
301 redirects are commands that you enter into your website's "htaccess" file. This file sits in your root directory, and is accessed before your website loads. That means that any commands in your htaccess file will trigger before your site pops up when someone visits it. You can edit your existing htaccess by simply using a text editor like Notepad.

301 Redirect Scenarios

The 301 redirects being reviewed in this tutorial are strictly for .htaccess file redirects and the Apache web server with the function mod rewrite turned on (this is on by default).


In each example, you'll see a shaded gray line with a command in it. You can simply copy that command and paste it into your htaccess file, under the line "RewriteEngine on" (this is mandatory). If "RewriteEngine on" doesn't exist in your htaccess file, simply type it in yourself.

Single Page Redirect

Here's the most simple scenario: 301 redirecting an old page to an existing page. In this case, we'll redirect "www.mysite.com/old.html" to "www.mysite.com/new.html":

Redirect 301 /old.html http://www.mysite.com/new.html


The first URL is the one being directed, and the second URL is the one it is being redirected to. Did you notice that the first link only requires you to type out the page, while the second link has the entire URL? This is the proper syntax, and it must be typed this way.

This also works for redirecting pages that have no extension, such as .html or .php - typically the kinds of pages you'll get when you use a content management system like Joomla or Wordpress. Using the above example, let's assume those pages have no ".html" after them. This is how we'd do the 301 redirect:

Redirect 301 /old http://www.mysite.com/new


Note that there is no "/" after the page name in that re-direct...we'll learn why, later!

Non-www to www

Every single website you create should use this particular one! Here, we'll re-direct the non-www version of your website (such as mysite.com) to the www version of your website (such as www.mysite.com).

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


In doing so, you're eliminating search engine issues that arise from having "two versions" of a website, which also taint analytics results.

Rename A Folder

Let's say you regretted naming a folder on your website, and wanted to change it. For example, you don't like "mysite.com/newcars/" and you wish had named it "mysite.com/cars/," because it's shorter and simpler. However, you want all of the pages in that folder to still work so that they also adapt to the new "/cars/" folder. This is what you'd use:

redirect 301 /newcars/ http://www.mysite.com/cars/


You can perform this redirect for deeper level folders, too. So, say you want the folder "mysite.com/cars/classic/" to move over to "/cars/all/classic/." You'd use this:

redirect 301 /cars/classic/ http://www.mysite.com/cars/all/classic/


Unlike previous examples in this tutorial which involved redirecting from one page to another, please note that you'll have to leave the "/" after folder names in a folder-to-folder redirect. That's because the trailing forwardslash denotes a "folder."

For example, this is a page: "mysite.com/test" and this is a folder: "mysite.com/test/"

Move A Folder

Unlike the previous lesson, let's say that I have a folder on my site: "mysite.com/cars," and I'm getting rid of that entire folder and all of the pages within it, such as "/cars/ford.html," "/cars/toyota.html," "/cars/acura.html" and several dozen others. Since I'm no longer using those HTML pages, I'll want that entire folder and its contents to move to "mysite.com/products"

redirectMatch 301 ^/cars/ http://www.mysite.com/products/


Similarly, say that you want to redirect an entire folder and all of its contents to a single HTML page, such as "mysite.com/cars" and all of its individual pages over to the single page "mysite.com/things/stuff.html"

redirectMatch 301 ^/cars/ http://www.mysite.com/things/stuff.html

Domain to Domain

Sometimes, a website needs to be moved over to a new domain name for whatever reason. It's a very simple thing to do, thankfully! Just copy and paste the code below (and absolutely nothing else), and make it be the sole contents of your htaccess file:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.new-domain.com/$1 [R=301,L]


There are two important things to note about this. For starters, this is somewhat of a "lazy" way to move a website. You're not moving the individual folders or pages over to their new counterparts on a new website; instead, you're just moving everything over to the home page of the new site. The alternative is to write a 301 redirect command for every single folder or page, if you really need to retain each page's search engine ranking.

Secondly, when you 301 redirect an old domain to a new one, be aware that you'll have to keep paying for the annual domain renewal and server account of the old domain name. Make it a point to change all of the backlinks that were pointing to the old domain, so that they point over to the new one. Once all backlinks are changed, you can consider dropping the old domain (unless it's a good domain that gets decent type-in traffic).

Example .htaccess

.htaccess file with 301 redirect commands

Additional 301 Info

301 Redirect Statements

  • [NC]: The statement [NC] can be used within a 301 redirect rewrite rule. It simply tells your server that the value before it is not case sensitive, and ignores differences between capitalized and non-capitalized letters.
  • [L]: This statement, when used at the end of a rewrite rule, tells the server that it is the "last line" in the set. Anything that comes afterward will not be processed in that set. For instance, "RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]"
  • Options +FollowSymLinks: You might see this in your existing htaccess file, before "RewriteEngine on". It's a statement that enables your "Mod Rewrite" function.

Verify that the 301 Redirect Worked!

To see if your 301 redirect worked, simply visit the page. No refresh is needed, since it's a server-side file that triggers immediately before the page elements start loading. Alternately, there are numerous "301 redirect check" websites out there on search engines that will run a diagnostic for you. You'll always want to manually check and see if it worked! An unnoticed redirect issue can lead to tragedy, including lost rankings!

301 Redirect Tips, Tricks and Info

  • 301 redirects are instant, because the .htaccess file is read before the page loads.
  • You won't need to do a 301 redirect to any pages that have no backlinks, technically...well, unless they have good search engine rankings. Otherwise, simply delete or rename the desired pages and re-submit an XML sitemap using Google Webmaster & Bing Webmaster.
  • When you've done a 301 redirect, check all pages involved: if you've redirected a main page, check both that page and several of its sub-pages to make sure they are behaving the way you've anticipated.
  • 301 redirects pass all page rank and SERPs (Search Engine Results Pages rankings) - making them a vital part of an SEO roadmap for a changing site. The passing of page rank from a 301 redirected page to another page will not be instantaneous.

More 301 Tips & Resources

Redirects - groups.google.com
A Google representative writes an insightful article about redirects and misconceptions.
Best Practices When Moving Your Site (Google Webmaster Blog)
What to do when you have a major 301 redirect task or a complete site move. Includes a checklist of everything that should be done for best results.

Comments & Feedback

submit

by

Pixelrage

A quick 301 redirect tutorial

Feeling creative? Create a Lens!