treehouse : what would you like to learn today?
Web Design Web Development iOS Development

301 Redirects

Last updated on:

This is the cleanest way to redirect a URL. Quick, easy, and search-engine friendly. Remember HTAccess stuff is for Apache servers only.

Redirect a single page

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

Redirect an entire site

This way does it with links intact. That is www.oldsite.com/some/crazy/link.html will become www.newsite.com/some/crazy/link.html. This is extremely helpful when you are just "moving" a site to a new domain. Place this on the OLD site:

Redirect 301 / http://newsite.com/
View Comments

Comments

  1. How would you code a 301 redirect for the following?:
    I want to redirect “website.com/” and “website.com/index.html” to “website.com” for the index page to maximize search engine points.

  2. Permalink to comment#

    Does this works the same when you use regular expressions? Maybe you should add another example.

  3. Permalink to comment#

    Yes, this is really useful.

  4. Thanks the first one is useful.. Was using redirection wordpress plugin but it was making my database bulky…

  5. Mitch
    Permalink to comment#

    yes, I have apache server, (VPS)
    yes, I do have other .htaccess files on it and they all work
    no – this redirect line doesn’t do a thing

  6. Works like a charm. Thanks for sharing!

  7. Permalink to comment#

    I simply use html redirect pages that look like this:

    Forbidden Area

    • Permalink to comment#

      Sorry html code wont show here:
      html
      head
      title Forbidden Area
      meta http-equiv=”refresh” content=”0; URL=http://www.The-Site-Name.com
      head
      body
      close body
      close html

    • Are you not losing vital link juice though, as that method won’t tell any existing links that are pointing to the site to look at the new one.

      Correct me if I’m wrong.

  8. This is the method I use for redirecting an entire site. It’s especially helpful when you have hundreds of possible URLs on one domain and you want to send them all to a new single location.

    redirectMatch 301 ^/ http://newsite.com

    Sometimes you just want to redirect from domain to domain with the permalink structure. Assuming you are able to use mod_rewrite, this should work.

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.oldsite.com$ [NC]
    RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]

  9. Keshav Naidu
    Permalink to comment#

    Yes this is usefull, but better to giv with DEMO..

  10. Hi how would you 301 redirect

    http://www.example.com to example.com

    I’m using Codeigniter framework, so i’m not sure if there’s a different process

  11. Thanks it works, I have used it in my website Jay Jalaram Extrusions
    By redirecting http://jayjalaramext.com/contact.html which is old one to the new one http://jayjalaramext.com/contact-us

    Check it.

    • Natalie
      Permalink to comment#

      Jay

      This is exactly what I am trying to do, but am grappling with the example here. I have pages like /contact.html and they need to go to /contact-us as in your example as well as a host of other pages where the file name was changed slightly or in some cases no longer exists.

      Can you provide an example of the code you used? Many thanks for any help

  12. Permalink to comment#

    @shaneka I think you are looking for the www to no-www snippet.

    http://css-tricks.com/snippets/htaccess/www-no-www/

  13. Daniel Winnard
    Permalink to comment#

    Hi,

    Just found this website for snippets. Awesome.

    I am having a problem which somebody might be able to help with.

    My company has just redeveloped our website. It is going all wordpress. However the initial site was in standard html and css. and we had a blog on a sub domain running wordpress.

    The problem now is we have deleted the sub domain for the blog as we are totally switching away from it. Some of its pages are listed in google. How would we redirect from blog.website.com to http://www.website.com using htaccess if no folder or sub domain now exists. Is this even possible?

  14. Will this work for changing “www.sameDomain.com/feature.html” to “www.sameDomain.com/feature.php” ? And would I need to specify every page that the extension has changed?

  15. Sithu raj

    Hi Richard Razo,
    For that canonicalization is the simple way.

  16. Permalink to comment#

    I want to block or redirect potentially harmful outgoing flash links that I have no control of from htaccess.

    Any Ideas ?

  17. You can block sites that you do not want to have in search engines in robots.txt.
    Cause google is the main engine you should also check your website in “google webmaster tools”.

  18. Permalink to comment#

    You can block sites that you do not want to have in search engines in robots.txt.

  19. Debangshu Nag

    I am having a problem

    Redirect 301 /old-folder/page.jsp http://new-site/new-folder1/new-folder2/3d0ce71b-13c1-a0b2-e7c9-5125e3851f2c

    Getting
    http://new-site/new-folder1/new-folder2/3d0ce71b-13c1-a0b2-e7c9-5125e3851f2c? /old-folder/page.jsp

    How can I remove the query string part that automatically concatenating.

Leave a Comment

Use markdown or basic HTML and be nice.