The Missing Guide: 301 Redirects

The modern web is fluid, with new pages being built and old ones being deleted on a constant basis. If you are planning to delete pages or reroute visitors from an old page or domain to a new one, this is the guide for you.

Why do I need a 301 Redirect?

301 redirects perform two major tasks: most importantly, they inform search engines that your page or site location has moved, and transfers the page rank and “link juice” from the old to the new. It’s a relatively simple solution to preserve a page or site’s hard-earned search rankings after it has moved. The second benefit is that every outstanding link or bookmark will still send visitors to the new site, so you don’t have to worry about broken links.

Note

This is strictly a guide for an .htaccess 301 Redirect, which is the most common server-side method. It is a requirement that your website be on an Apache server with Mod Rewrite turned on; most hosting companies use this, but if you aren’t sure, check with your host.

First, you need to force your computer to recognize the .htaccess file, the file we will be editing in later steps. By default, any files beginning with a “.” are hidden on most computers because they are usually sensitive operating-system files that would cause damage to the system if accidentally edited or deleted. However, we need to be able to see and edit a “.” file for our website, so here’s what you need to do:

Mac Users

1. Go to Applications>Utilities and open Terminal. With the window selected, paste the following and hit Enter.

defaults write com.apple.Finder AppleShowAllFiles TRUE

2. Force Finder to relaunch by pasting the following code and hitting Enter again.

killall Finder

3. When you are finished and want to hide system files again (which is generally a good idea), repeat the same steps, but replacing TRUE with FALSE.

defaults write com.apple.Finder AppleShowAllFiles FALSE

Then force Finder to relaunch again:

killall Finder

PC Users (Windows Vista & Windows 7)

1. Launch Windows Explorer.

2. In the Organize drop-down menu, choose Folder and Search Options.

3. Under the View tab, scroll down and check the box next to “Show hidden files and folders.”

4. To hide the files again, simply go back and uncheck the box.

Opening the .htaccess File

Now that you can actually see the file you’ll working on, go to your site’s root directory, or topmost folder. There you should find the .htaccess file. If you don’t have one, I’ll explain how to create one.

If There is No .htaccess File

Never fear, creating one will be a quick and painless:

1. Go to any basic text editor like Notepad or TextEdit, and open a new document.
2. In the first line, write the following:

RewriteEngine ON

3. Go ahead and Save As, and make sure the file name is simply .htaccess, with no .txt or anything after it. If you followed the step above to allow your computer to view hidden files, this should be no problem.

That’s it! You’re set up to begin the next steps.

If There is an .htaccess File

1. Download it and open it in a basic text editor like Notepad or Text Edit.
2. Ensure there is a line that reads:

RewriteEngine ON

If that line isn’t there, type it in on the first line. Make sure any code you add is below this line.

Adding the 301 Redirect

The next step is to add the code that will perform the redirect. While there are many types of redirect, we’re only going to look at redirecting single pages and domains. If you need a different type of redirect, a quick search should yield a snippet that you can paste in place of the below snippets.

Page to Page Redirect

To redirect from an old or deleted page on your domain to a new one, paste the following code into your .htaccess file below the Rewrite Engine on line, replacing the information to reflect your site and page names:

Redirect 301 /oldpage.html http://www.yoursite.com/newpage.html

That’s it! The old or deleted page is the first item, and the full address of the new page follows.

Domain Redirect

If you changed web addresses, you’ll need to direct search engines and traffic to the new one. In this case, you’ll need to start a new blank .htaccess file and paste the following:

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

This .htaccess file will be placed on your old domain, so this will only be effective as long as the old domain stays live. It’s suggested that you keep your old website live until search engines recognize the 301 and credit the new page with rankings.  For more information on helping Google recognize your new site, I recommend this article on how to properly move domains from an SEO standpoint. In the meantime, it’s important that you contact any site or directory containing backlinks to ensure they have the new URL for your site.

Uploading and Testing

Good news—you’re almost there! Your last step is to upload the new .htaccess file to your website’s  root directory. Once that is done, be sure to test and make sure the old address redirects you to the new page or domain!

Congratulations—you’re now a 301 veteran.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • Luke

    For a domain change redirect, can’t you just use the same code as you mention for the page-to-page redirect, but just in the following format:

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

    That’s what I have read elsewhere…

    • http://www.rypmarketing.com Adam Thompson

      I believe the line you referenced would only redirect the homepage, whereas the code in the article would redirect all pages on the domain to the equivalent url on the new domain.

      • Luke

        Nope. This code will redirect all pages within the old domain to the new domain.

        You can be as granular as using it to redirect individual pages (simple replace the “/” after “301” with the specific page to be redirected and indicate the new target and it will redirect for that page only).

        You can also specify a folder by replacing “/” with “/oldFolder/” for instance, and any pages within that folder will also be redirected according to this rule.

  • brothercake

    Much easier to do it in PHP — just leave the old page in place for a couple of months with a 301 header() and location() redirect

  • http://wpsites.net Brad Dalton

    The Seo article you linked to on SEOMoz is incorrect
    When you change domains you lose all your search engine traffic and start from scratch
    You don’t get the Domain authority or page rank from your old domain back
    Even though the links are permanently redirected, they are diluted so much that any good rankings are lost so its highly advisable to stay with your old domain if you can

    • http://www.rypmarketing.com Adam Thompson

      @Brad, a correctly redirected domain *will* transfer the majority of link juice to the new domain, allowing you to keep most of the rankings and traffic. I’ve done it multiple times and maintained the rankings without a hiccup. If you’ve done a transfer and lost all your rankings, something was probably not setup quite right. :-)

      Matt Cutts stated in this video that you will only lose a “tiny” bit of link juice/PageRank when you do a 301 redirect: http://www.mattcutts.com/blog/rel-canonical-html-head

  • http://www.diybackyardworkshop.com Brad Smith

    I’ve seen WordPress redirection plugins? Is there an advantage using them vs htaccess?

    • http://www.rypmarketing.com/ Mandy Barrington

      Hi Brad! I think it’s a matter of looking carefully at the features to ensure the plugin is made for the type of redirect you’re needing (i.e. full domain redirect vs page redirect). The ones I looked at seemed be PHP redirects. After doing some research, I haven’t found any evidence to say that a plugin redirect is less effective than an .htaccess redirect, but that’s not speaking from experience (I’ll be honest—I’m not a WordPress expert :) )

  • Donna

    Agree with brothercake … if you have a lot of changes, then if at all possible it’s better to replace the pages with 301 redirects. Otherwise you add a long list of checks on every page view to your .htaccess file. Here’s an example of the PHP code you need.

    header('HTTP/1.1 301 Moved Permanently');
    header('Location: /about/');
    exit(0);

  • http://www.rypmarketing.com/ Mandy Barrington

    This is why I love Site Point—lots of perspectives to learn from!
    @Brothercake and Donna, I agree, there is definitely an argument for using PHP redirects (and thanks for the code, Donna). I kept the focus to .htaccess redirects because for the purposes discussed (a deleted page or moved domain) this server-side method is an efficient and common solution. PHP redirects would be a helpful article topic for the future!
    @Luke, this is the tried-and-true method for me; would you mind sharing your source?
    @ Brad Dalton, thanks for your input! It can take time for a search engines to recognize the 301 redirect, but my understanding is that they do preserve rankings. Where did you read otherwise?
    @Brad Smith, great question—I’m not familiar with the WordPress redirect plugins, I’d have to do some research on that one!

  • http://wpsites.net Brad Dalton

    Didn’t read otherwise. I tried it for one of my blogs and review the changes every day

    I have written many articles showing real screen shots about it as well http://wpsites.net/seo/what-happens-to-your-keyword-search-queries-when-you-change-domains/

    The 301’s are recognized immediately but that doesn’t mean anything other than redirecting traffic from the old link to the new site

    Why do u think 301’s preserve rankings?

    My articles show proof that what i stated previously is true. Traffic down over 80% for 1 month after the change.

    • http://www.rypmarketing.com/ Mandy Barrington

      Hi Brad! Nice article—it’s very helpful to see different case studies. As I said before, I’m with you that rankings can take various amounts of time to catch up to where they were before. One source I’m referring to is this article from SEOmoz:

      “Be aware that when moving a page from one URL to another, the search engines will take some time to discover the 301, recognize it, and credit the new page with the rankings and trust of its predecessor. This process can be lengthier if the given web page if search engine spiders rarely visit it or if the new URL doesn’t properly resolve.”

      Although it takes time, the redirect does, in fact, pass ranking.

  • http://wpsites.net Brad Dalton

    Adam

    The rankings will come back yes. But will take 3- 6 months

    You don’t get instant PR transferred to a new domain

    Its been 1 month exactly since i changed domains and correctly redirected all URL’s to exactly the same content on the new domain

    The old site now has NO traffic and the new domain has 80% less which is caused by the loss of search engine traffic which was 80%

    Those are the facts. The change was done correctly but i don’t expect the same PR until the next update in Feb therefore my keyword positions in the SERPS won’t be the same until after 3 months from the date of domain change

    • http://www.rypmarketing.com Adam Thompson

      Brad, each time I’ve moved a site the rankings have rebounded extremely quickly with little to no “downtime” at all. I know other SEOs have reported the same thing. So losing all your rankings for 3-6 months is not a given. Since this is what happened to you, you might want to see if you can find anyone else that has experienced the same lag you are seeing and found a solution. :-)

      By the way, Google updates their real PageRank numbers and site rankings daily – it’s only the toolbar PageRank that is only updated a few times a year. The toolbar Pagrank is just an approximation of the real Pagerank and doesn’t affect site rankings directly. So there’s no reason to assume your rankings won’t update/improve until the toolbar PageRank changes.

      You might also check out http://www.seerinteractive.com/blog/301-redirect-test-how-much-link-juice-are-you-losing/2010/04/09/

  • Graham Alexander

    A note for Mac Users new to fiddling with .htaccess files. Instead of typing esoteric code in Terminal you could use the TextWranger editor from Bare Bones software which can “see” invisible files or use TinkerTool by Marcel Bresink which does the same job you’ve outlined here by making the files visible. Both are free and excellent tools.

    • http://www.rypmarketing.com/ Mandy Barrington

      I’ll check those tools out—thanks Graham!

  • http://wpsites.net Brad Dalton

    Adam

    That article you linked to shows NO real screenshots proving you don’t lose any search engine traffic when changing domains.

    Even Google states it will take sometime before all your URL’s are indexed

    “While it can be some time before Google crawls your old and new sites, and all these changes take effect”

    My sites never suffer any downtime when changing domains so not sure why you refer to downtime

    Show me some proof that what you are saying is true

  • http://workingtraffic.com Mark Underwood

    PathFinder allows you to see invisible files as well and is also a much better file manager than Finder (although that is faint praise indeed). You can even actually delete files with PathFinder…amazing !…no I am not affiliated wit PathFinder.