18 Jan 2012

The Ultimate How-To Guide on 301 Redirects

Many of our clients comment on our site review reports on subjects like canonicalization, site migration, architecture changes, or page updates. Specifically when we advise implementing 301 redirects to optimize and maintain the value of a specific URL in search engines, inevitably they reply with the following question: “How do you implement a 301 redirect?”

As long as you have access to the root directory of your web site on your web server, it’s not hard to do. We’re going to assume your site is running an Apache web server (most of the world is), so let’s have a little tech talk on implementing 301s in Apache.

First, a little background

A redirect, at least in terms of websites, is a way of automatically transferring the end user from one URL to another. While there are several technical methods of implementing redirects, for search engine optimization (SEO) purposes, we advise using an HTTP 301 permanent redirect.

Unlike its counterpart, the default HTTP 302 temporary redirect, the 301 indicates the old, linked URL is no longer in use (whereas the 302 indicates the old URL is merely offline temporarily, but is expected to return in the future).

The differentiation between the 301 and the 302 is most important for search engine crawlers. When the crawler encounters a link to your site whose URL is configured with a permanent redirect, your web server (Apache) responds with a 301 status code and then redirects the crawler to the new URL. The search engine not only accepts the redirect to the new URL, but then also begins the process of transferring any existing page rank value from the old URL to the new one in the redirect (it’s that second part of that process that’s so important for SEO, and that’s not done with a 302).

The transferal of page rank value between URLs in the search engine index is not an overnight process, but rest assured, if you’ve done your 301 homework correctly, your site updates will soon be reflected in the search engine results pages (SERPs) without a hitch.

Comparing 301 redirects to rel=”canonical” tags

Some folks know that all of the major search engines announced in early 2009 their planned support for a new HTML tag attribute. Within the <link> tag (found in the <head> section of the page code), they promised support for the new attribute “rel”, used along with the associated value “canonical”, followed by the standard attribute “href” and a URL for its associated value. For example:

<link rel="canonical" href="http://www.xyz.com/page.html" />

The intention of the rel=canonical tag is to inform the search engine crawlers of the single primary (aka canonical) URL for the page content. This is very helpful when a site uses dynamic attribute variables in its URLs. Dynamic URLs can lead to the indexing of multiple versions of a URL for any given page, resulting in index content duplication (which search engines hate!).

While the use of the <link> tag with the rel=canonical attribute is helpful for a page, it’s not a reliable substitute for 301 redirects. Here are my reasons for this:

  1. For starters, in early 2009 and for a long time afterward, Google was the only search engine fully supporting the use of the rel=canonical tag. This has been improving slowly over time, but there still may be questions about how strongly each search engine accepts and uses the tag’s data. On the other hand, all of the search engines fully support 301 redirects.
  2. Secondly, rel=canonical tags are still regarded by the search engines as hints or suggestions, not firm directives. On the other hand, 301 redirects are firm instructions.
  3. Lastly, unlike 301 redirects, which can be configured in one file for the whole site, each webpage needs its own edit to add the new line of custom code for the rel=canonical tag.

That all said, the use of rel=canonical tags can be beneficial to redirecting blog pages (assuming you have access and the technical skill to edit the blog’s source code for a given page’s <head> tag module). In this edge case, the use of a 301 can mean the loss of the original page’s associated user comments and social shares, which all have their own SEO value.

We recommend that clients consider using rel=canonical tags to help minimize indexed content duplication. Regardless for standard matters of reassigning the function and search index values of old URLs to new, we advise clients stick with tried and true 301 redirects.

How do you implement a 301 in Apache?

An Apache web server can implement 301 redirects through script code modifications to one of two of its text-based configuration files: either .htaccess (for directories representing separate sites on one server) or httpd.conf (at the root of the Apache installation). Typically the .htaccess configuration method is used, so we’ll focus on that here.

There are a number of different circumstances for which implementing a 301 redirect is recommended. The specific code you need to create differs by situation, so let’s cover how to implement a 301 for each possible scenario.

Start your rewrite engines

The first thing you need to do is open the text-based file called .htaccess found in the root directory of your site on the Apache web server. Be sure you only do so with a plain text editor application, such as Notepad on Windows-based computers.

Once open, before you add any scenario-specific custom code, you need to do two things:

  1. Enable the Apache mod_rewrite module.
  2. Enable the ReWriteEngine in the mod_rewrite module.

Add the following two lines of code to do this:

Options +FollowSymLinks
RewriteEngine on

Note that this pair of lines is only needed once in the .htaccess file. Once they are  added, you are ready to add the custom 301 redirect code for your scenario.

The following redirect scenarios use placeholder data as file, directory, and domain names in the sample code. Of course, be sure to substitute your own site data! These are not explicit code samples!

301 a single page URL to another

To create a 301 redirect from one URL to another URL, add the following line of code:

Redirect 301 /retiredpage.html http://www.xyz.com/newpage.html

You can add as many of these redirect lines as necessary to the .htaccess file.

301 a directory URL and all of its contents to another

If you have redesigned your site architecture and renamed a directory, you need to create a 301 for the entire directory. Here’s how:

RedirectMatch 301 ^/oldname/ http://www.xyz.com/newname/

301 a domain name URL to another

If you just bought an aged domain name whose traffic (and search page rank value) you want to use to augment that of your existing site’s domain, you can set up a 301 to transfer all traffic and ranking from the purchased domain name to your current site. Use the following code as an example:

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

Be sure you set up this redirect code in the .htaccess file of the source site you want redirected, not the redirect target site!

301 domain name URL variants for canonicalization

Since search engines index URLs, having multiple URLs in the index that point to the same content page divides the available page rank credit for that page among those URLs. This is definitely a “not optimized for search” state of affairs! To learn more about the details of canonicalization, take a look at the Search Engine Land post Why Canonicalization Matters From A Linking Perspective. The bottom line is you want to consolidate the page rank to one (canonical) URL to optimize the search value of that content.

Once you understand canonicalization best practices, you’ll want to implement them on your site. That means you must account for all redirecting possible alternative URL variations to the canonical URL. Use the following code sample for your site’s home page:

RewriteCond %{HTTP_HOST} ^xyz\.com [NC]
RewriteRule ^(.*)$ http://www.xyz.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(default|index)\.(html|php|htm)\ HTTP/ [NC]
RewriteRule ^(([^/]+/)*)(default|main|index)\.(html|php|htm)$ http://www.xyz.com/$1 [L,R=301]

The first two-line block of code redirects URLs that have omitted the “www.” prefix to the full “www.xyz.com” home page URL. That means the home page URL http://xyz.com will not resolve on its own, but instead will redirect to http://www.xyz.com/.

The second code block redirects URLs specifying default page references to the URL that omits default page reference names. This code ensures that any home page URL that includes several versions of explicit page name references, such as default.htm or index.html, will be redirected to the canonical home page URL, http://www.xyz.com/.

Documenting and testing your work

We strongly recommend that you add documentation lines to your coding work in the .htaccess file. To do so, simply start a line in the left margin using the # character, such as in the following code example:

# Redirect this entire domain, abc.com, to the domain xyz.com
RedirectMatch 301 ^(.*)$ http://www.xyz.com

Good documentation will always help you (and those who will have to maintain the site after you leave!) understand what the code underneath was intended to do. This information will help with troubleshooting when things don’t work as expected or changes have to be made.

Of course, you should test your changes as well. Once you’ve used FTP to upload the revised .htaccess file to the root directory of your site, it is ready to go. Use your browser to attempt to access the URL of the page for which you’ve set up the redirect. It should immediately redirect to the new URL as expected. You may also want to test the redirect with online tools such as Redirect Check SEO Tool or perhaps better yet, the Internet Marketing Ninjas Header Checker tool, which not only follows all of the redirects for a given URL and reports the HTTP status codes returned, the tool also provides HTTP status code definitions. Sweet!

Troubleshooting problems

If the redirects are not working as expected, it’s time for troubleshooting.

First off, if the redirect coding in your .htaccess file is valid but it’s not working, check the installation status of the mod_rewrite extension in Apache. This module is typically installed by default, but if it’s not there, the .htaccess coding examples provided above won’t work. Also ensure you added the two lines of code that enable both the mod_rewrite module and the ReWriteEngine.

Also, note that the use of [NC] in RewriteCond lines indicate that the input values are not case specific. If you omit this, URLs in all capital or mixed case letters may not work as expected. Additionally, the use of L in the code [L,R=301] tells the file interpreter engine that this is the last line of code processed for matching input. If you have conflicting statements in your .htaccess code, use the L code in the line that takes precedence.

Lastly, note that stacking up 301s over time is not a great strategy for website health. 301s are very useful, but you’ll want to update your site’s internal links to the correct URLs. Ignoring your old intra-site links in favor of simply using 301 after 301 after 301 only slows down your webpage load time, which is bad enough for SEO. And if the redirects are stacked deep enough, search crawlers may not follow them all. If that happens, it’ll damage your site’s ability to stay in the search index. Also, when you are updating your site links, you should also ensure your sitemap.xml file is updated with the newest URLs.

Taking control of your site’s URLs with permanent 301 redirects is a standard best practice in white hat SEO. Make sure you let the search engines know how to get to your pages while ensuring any earned page rank value is invested in the active pages on your site.


  1. David Ewing January 18, 2012 at 11:10 AM

    This article couldn’t have come at a better time! We are in the process of transferring to another enterprise level CMS and this cleared up many issues with whether or not to 301 or 302 the URLs

  2. Nick Sparagis January 23, 2012 at 3:48 PM

    Hi Rick,

    Got a question, we have a site that we’re building up. One of our strategies is to buy value priced websites that rank for similar keywords to our main site and then do a 301 redirect. This passes the backlinks and most of the SEO of the purchased site onto the main site. Well, it’s working based on opensiteexplorer.org data and our main site is being pushed up for a lot of the common keywords. My concern is that this site will lose strength in the future b/c none of it’s actual pages (site being redirected) are still being crawled. Do you think this is solid SEO tactic? Thanks.

    1. Rick DeJarnette January 24, 2012 at 5:43 PM


      Thanks for writing! Nice strategy. I’m glad to read that you are enjoying SEO success as a result. As for your concern about the redirected sites losing SEO strength, think of your websites metaphorically as buckets of water. The 301 redirect of the old site bucket is like pouring its SEO water into the bucket of your main site (maybe with a little splashing for fun). In the eyes of the search engines, the old site has been superseded, so it’s not going to keep its value – it gave it up with the 301. That redirect a permanent transferal of SEO rank and trust.

      However, as long as those old, deprecated sites still have backlinks, maintaining the old sites and the 301 you set up for them is important. You could contact the webmasters of those sites linking to your redirected sites to ask them to update their links. You’ll likely only have moderate success at best with that effort (you’re asking for free work!). That said, if you’ve improved or updated the original content to which they linked, that fact might spur them on to update the link. Then again, if they were friends with the site’s original owner, they might also simply take down the link. You need to assess if the effort is worth the risk versus the potential benefit. Do note that per Matt Cutts, there is a small amount of PageRank decay when using a 301 (http://www.stonetemple.com/articles/interview-matt-cutts-012510.shtml), so getting a link updated to the new site is preferable. That said, that redirect “tax” on PR is a small price to pay for the overall benefits of using 301s to enhance traffic flow to your main site.

  3. Fidel May 22, 2012 at 1:37 PM

    This worked perfectly Rick. It was well explained and when i implemented it on my site there no issues. I had 1 domain and while doing SEO work i realized that i was getting different links for http, www. and non-www links. I used this 301 redirect to consolidate them to one URL. Hopefully this will stop that issue i was having.

    Thanks Rick

  4. May 30, 2012 at 3:54 AM

    Thank you for the information you shared, I wish to know in windows server where can I rewrite the URL?

  5. Cloud Computing Companies June 12, 2012 at 5:12 AM

    A blog post with good information.
    Are URL Aliases same as 301 redirect?

    To get into a little brief, i would like to know whether URL the value (as in SEO like page rank, search rankings etc.) will remain the same if the page URL has been changed by using URL aliases and not 301 redirect.

    301 Redirect is permanent redirect, and it passes the complete value and authority of that page to the new URL.

  6. Amit Sharma June 18, 2012 at 12:45 AM

    Does a newly created blog have to do anything about 301 redirect ?
    Blog is on WordPress, started with a brand new domain and using xyz.com as a preferable domain.

  7. September 13, 2012 at 9:20 AM

    i have read your blog ref of http://www.seomoz.org/blog/common-technical-seo-problems-and-how-to-solve-them

    But, here I have a question, if I will do 301 for specific page to new page than how many days google take to pass link juice?

  8. Abdul Sarif October 30, 2012 at 2:15 AM

    I want know. how can i fix my canonicalization. please explain me.

    1. Alert eMarketing Malta February 14, 2014 at 9:50 AM

      IIS by default uses a 302-redirection (unless checking the option for permanent redirection) and in asp.net the Response.Redirect command does a 302-redirection. Hence, it is easy to get them implemented incorrectly during development!

  9. Roberta Selfridge December 24, 2012 at 11:15 PM

    This one little snip is what I fixed what I have been working on for HOURS! I had to make my ,htacces file and no one else gave this with their instructions. Thanks so much and Happy Holidays!

  10. ugo January 8, 2013 at 8:23 AM

    Thank you, that was exactly the right solution for me!

  11. James Arrondelle February 26, 2013 at 2:28 PM

    Tearing my hair out trying to get the “301 domain name URL variants for canonicalization” section to work! I’m actually working in my http.conf file, having done simple 301 re-directs with no problem. Would I need to make any changes to the code you provide? Any help MUCH appreciated!

  12. Julie March 21, 2013 at 5:45 PM

    Well played, internet marketing ninjas.

    Because this was helpful. I’m either gonna fix everything, or crash my site trying. At this point, both are a win.

  13. SEO May 1, 2013 at 9:53 AM

    That basically explained everything i need to know in regard to redirects. Thanks 🙂

  14. May 3, 2013 at 1:17 PM

    Thanks Great article Rick. 2 things, is the xyz\.com in line 1 of the canoncilization example a typo? it should be xyz.com correct (no slash)?
    Also, suppose I want to do exactly what your 4 lines do in the canonicalization section, but wanted to do this via a web config file instead of htaccess. How would one do that?


  15. Renee Skolnick June 3, 2013 at 9:55 AM

    Thanks for the code for non www to www. This is the first out of about 10 I’ve tried that actually works. Thanks! Could you show how to redirect a dynamic url to another dynamic url. For instance, example.com/tag/anything.html to example.com/tags/1/anything.html. Thanks again!

  16. Mark from Malta October 23, 2013 at 10:15 AM

    One must remember that a 301 don’t transfer all the link juice though, but most. Also, I would use only the canonical tag if you want to preserve old content, otherwise 301 is always best way to go.

  17. Johnny Rogers December 17, 2013 at 12:32 PM

    Been working with a client that uses HP/Autonomy TeamSite and we have been trying to implement 301 Redirects, but they are telling us the following:

    ” We use an Apache Web Server redirect and we centralize the setup of the redirects within the Apache config instead of various .htaccess files to ease the administration of the redirects. Since we are using Apache Web Server redirects that URL does not change for the user but they are going to the redirected page.”

    Question: Should this matter? Shouldn’t they still be able to set basic/standard 301 Redirects?

  18. Bill Zientek February 4, 2014 at 3:59 PM

    This is a great helpful post to assist all those who haven’t set up 301’s on a regular basis. In WP, it’s obviously much simpler, but always good to know how to hand code a redirect!

  19. Adam Lurie April 15, 2014 at 8:48 PM

    No matter how many times I implement a redirect its always nice to cross reference your code and just make sure your getting it right, to avoid a 500 error so as always I searched Google and this time I ended here, great tips and one for the bookmarks, thanks Ninjas

  20. May 23, 2014 at 8:48 PM

    Thanks for being clear & concise. I have everything ready to go into my .ht access file. Your article was the only one I found that mentioned the engine rewrite code for Apache.

    I do have a question. Because my site update was so overdue, the structure is quite different on the new one. Is it OK to direct multiple pages from the old site-to one new page? I am consolidating in some areas, and expanding in others.

    Also I have a couple of old sub-domains I am integrating onto the new site-I am guessing separate .htaccess 301’s from old subdomain to new domain?

    Thanks for your article.


  21. Neal Conroy July 7, 2014 at 1:55 PM

    How can you forward if the prefix of the domain name changed?

    For example:

    RedirectMatch “http://domain.com/*” “http://new.domain.com/*”


    Redirect 301 http://domain.com/* http://new.domain.com/*



  22. Lalit November 29, 2014 at 12:30 PM

    Hi Rick,

    This is really a helpful article. I solved a lot of problems with the help of your suggestions which my developer could not. I have still some URLs which are not being directed using 301. I have listed below URLs which are not being redirected. I guess the underscore ‘_’ in the URL is the reason of problem. I will be waiting for your kind suggestion. Thanks, Lalit

  23. Kamalakannan K December 22, 2014 at 8:07 AM

    Hi All,

    Does any one have an idea how to use RedirectMatch based on HTTP headers. Say for example, I want to set the response code as 301 if the request header has Range header / some header

  24. Arielle Arth March 8, 2015 at 12:10 PM

    Hi Rick, Loved the informative, clear Ninja article about redirects, but I have a few questions. I would love your help. I built a website for a friend who has 5 locations, and recently uploaded a 301 redirect. The site is one year old and this is the code I added to the Apache server: It does redirect. (I sent you a mes on your Gplus account with more info.)

    Options +FollowSymLinks

    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^www.example\.com$ [NC]

    RewriteRule ^(.*)$ http://example.com$1 [R=301,L]

    1. The landing page canonical has no backslash after com
    -Then is the RULE correct deleting the backslash?
    -Is it better to include?
    -Or do I include it it only on sites that the canonical has a backslash. -And what does the backslash do?
    -Does Google and other search engines read it the same?
    -Is it better to use a backslash on the canonical for any site, not just this one?

    2. I also want index.htm to redirect. This code does not redirect it,
    -or does it?
    -Also, once on site, when clicking back to home. The URL in Google now includes the /index.htm. I want it to read example.com
    -How do I do this (one of your examples appeared to)

    3. Secondary pages all end in .htm and are canonical without the backslash.
    -Should I keep it this way?
    -And is one better than the other (slash, no slash)
    -Is a backslash better to use on any new (different URL) I may build?
    -Will my current redirect, redirect these pages to the non WWW?
    -And will images be redirected properly?

    Lastly, Google sent a duplicate content mes. for the www and non www for the main page as well as for all 5 local landing pages.
    -Will my code address all these issues.
    -If not then how.
    If I search site: example.com, all the local pages have plummeted. They have also fallen in Google search.

    Your help will be greatly appreciated. I just do not know what to do next. I noticed you are into film. My friend is a Hollywood screenwriter and she shares like crazy.
    Please get back with me soon. Arielle

Leave a Reply