Code Snippet

Home » Code Snippets » HTAccess » 301 Redirects

301 Redirects

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/

Subscribe to The Thread

  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. Does this works the same when you use regular expressions? Maybe you should add another example.

  3. 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

    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. I simply use html redirect pages that look like this:

    Forbidden Area

    • 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

    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

      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. @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

    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?

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~