Grow your CSS skills. Land your dream job.

Last updated on:

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/

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?

    • Permalink to comment#

      Yes you would use Redirect 301 /oldpage/ http://example.com/newpage/ as I need to do exactly the same thing on a site I’m converting to WordPress. It is precisely because the old url doesn’t work that you tell the search engines what the new url is to replace it. Did you manage to get this working?

      The issue for me is the amount of time it is going to take to redirect every single old page to the new page as the old site structure was different, sigh..

  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.

  20. The question sometimes arrises with clients “How permanent is a 301 permanent redirect?” Anotherwords, if at some future point, the client changes their mind for whatever reason, what are the consequences of changing a “permanent” redirect?

  21. What is the basic difference between a mod rewrite and a 301 rewrite? Ive been using mod rewrite method in htaccess.

  22. Hi , I got this code to redirect domain.com/index to domain.com, but does not work. Can anyone tell me why?
    Below is the code

    RewriteCond %{THE_REQUEST} ^./index
    RewriteRule ^(.
    )index$ http://www.gsblagna.com/$1 [R=301]

  23. Adam

    thanks you very helpful i used the simple one line

    Redirect 301 /sample.html http://www.newsite.com

    thanks for sharing

  24. Narcís
    Permalink to comment#

    Could this be applied to redirect from non www to www ? Something like:
    Redirect 301 myDomain.com http://www.myDomain.com

    or the short version: Redirect 301 / http://myDomain.com/

    Could this work? is it necessary to include http:// ?

  25. ayyan
    Permalink to comment#

    Well i am transfering all of my data from old site to new one. including posts. i.e. i want to on site A there was a page called /mypage.html and i want that when user click on this url from search page it will be redirect to new site containing same page. I tried to do a 301 redirect using ” But its redirecting only main domain, please help me how can i redirect posts too? Regards

  26. Redirects are great in terms of SEO and specifically 301 as opposed to 302. That said, you need to refrain from using redirects whenever possible because they don’t really retain all your Google juice. Just be sure to use it as last resort rather than a primal one. Again, I’m talking specifically about the search engine optimization aspect of the matter.

  27. Permalink to comment#

    This is a must if you’re moving pages and don’t want to have a bunch of dead links. I use SEO spider to map all URLs on old website and then create 301 redirection for the one that don’t match anymore.

  28. Sandeep
    Permalink to comment#

    I used
    RewriteCond %{THE_REQUEST} \/index.html\ HTTP [NC]
    RewriteRule (.*)index.html$ /$1 [R=301,L]

    to remove index.html,
    and to redirect used
    Redirect 301 /old-file.html http://mywebsite.com (It works)
    But issue is,
    I just want to redirect

    Redirect 301 http://mywebsite.com/education/ http://mywebsite.com/education/why-go-waterless.html but doesn’t work. Anyone help please.

  29. Vinny
    Permalink to comment#

    Someone please help me. I have a website that was done in coldfusion and I am trying to redirect to an html page. I have two problems, however: one problem is the following:

    Redirect 301 /tax_glossary.cfm http://www.mysite.com [R=301,L]

    and the other is:

    Redirect 301 /Margin-Accounts.9.htm http://www.mysite.com/margin-accounts.html [R=301,L]

    I have this at the beginning of the htaccess file:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule http://www.mysite.com [R=301,L]

    I keep getting an internal server error.

    Any help would be greatly appreciated.

    Thank you

  30. Ben Racicot
    Permalink to comment#

    What about 301 redirects with masking enabled?

  31. Ben M
    Permalink to comment#

    How do I get this to work when the directory being redirected has hyphens? So:

    http://www.example.org/about/move-consultant-team/name-ofperson

    to

    http://www.example.org/about/consultants/name-ofperson

  32. Permalink to comment#

    Thank you but how do i do some thing like

    redirecting

    site1.com/pagename.html

    to

    othersite.com/pagename.html

    and i have many urls like this with only domain change

Leave a Comment

Current day month ye@r *

*May or may not contain any actual "CSS" or "Tricks".