Treehouse: 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
Redirect 301 /oldpage2.html

Redirect an entire site

This way does it with links intact. That is will become This is extremely helpful when you are just "moving" a site to a new domain. Place this on the OLD site:

Redirect 301 /


  1. Richard Razo
    Permalink to comment#

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

  2. Gabe
    Permalink to comment#

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

  3. Bhavya
    Permalink to comment#

    Yes, this is really useful.

  4. Themepremium
    Permalink to comment#

    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. Priyank Sharma
    Permalink to comment#

    Works like a charm. Thanks for sharing!

  7. Tim
    Permalink to comment#

    I simply use html redirect pages that look like this:

    Forbidden Area

    • Tim
      Permalink to comment#

      Sorry html code wont show here:
      title Forbidden Area
      meta http-equiv=”refresh” content=”0; URL=
      close body
      close html

    • SEO Essex

      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. Ryan Burnette

    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 ^/

    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} ^$ [NC]
    RewriteRule ^(.*)$$1 [R=301,L]

  9. Keshav Naidu
    Permalink to comment#

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

  10. Shaneka Landell
    Permalink to comment#

    Hi how would you 301 redirect to

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

    • Lohith M
      Permalink to comment#


      try this in .htaccess file

      RewriteEngine On

      RewriteCond %{HTTP_HOST} ^
      RewriteRule ^(.*)$$1 [R=301,QSA,L]

      It might work for your query..

  11. Jay Jalaram Extrusions
    Permalink to comment#

    Thanks it works, I have used it in my website Jay Jalaram Extrusions
    By redirecting which is old one to the new one

    Check it.

    • Natalie
      Permalink to comment#


      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. Dan
    Permalink to comment#

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

  13. Daniel Winnard
    Permalink to comment#


    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 to using htaccess if no folder or sub domain now exists. Is this even possible?

    • Paul
      Permalink to comment#

      Yes you would use Redirect 301 /oldpage/ 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. Dan Cornock
    Permalink to comment#

    Will this work for changing “” to “” ? 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. Ray
    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. Izdelava strani
    Permalink to comment#

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

    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. Rayan beck

    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. Roy M J
    Permalink to comment#

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

  22. Vishal

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

    RewriteCond %{THE_REQUEST} ^./index
    RewriteRule ^(.
    )index$$1 [R=301]

  23. Adam

    thanks you very helpful i used the simple one line

    Redirect 301 /sample.html

    thanks for sharing

  24. Narcís
    Permalink to comment#

    Could this be applied to redirect from non www to www ? Something like:
    Redirect 301

    or the short version: Redirect 301 /

    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. Kenneth von Rauch
    Permalink to comment#

    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. Mara
    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 (It works)
    But issue is,
    I just want to redirect

    Redirect 301 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 [R=301,L]

    and the other is:

    Redirect 301 /Margin-Accounts.9.htm [R=301,L]

    I have this at the beginning of the htaccess file:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule [R=301,L]

    I keep getting an internal server error.

    Any help would be greatly appreciated.

    Thank you

    • Usama Ejaz

      remove “L” parameter.


      RewriteRule ^branches/kl$ /branches.php?s=kl [R=301]

  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:


  32. Nitya
    Permalink to comment#

    Thank you but how do i do some thing like



    and i have many urls like this with only domain change

  33. Jason
    Permalink to comment#

    I personally think using the htaccess method works better. Always do this on all my sites and have had no problems.

  34. Robin

    How to redirect this url
    from (url not working)
    to (live url)

  35. Loki


    i have a query regarding redirection.
    i have site A and right now i redirected to Site B 301 permanent redirection.
    Now in site A i have a folder called “test” but that should not get redirect to Site B…
    how to stop avoiding from site A/particular folder to site B…
    we don have that folder in Site B so i am getting not found error.
    and i don want that folder url should not get redirect.. how to over come from this prob…
    Need help …………

  36. Patrick McCoy
    Permalink to comment#

    If you’re keeping your same domain name and moving from a HTML site to a WP site, we’re manually coding in the 301’s. But, we have a WP blog on this HTML site that we added about a year ago, so the URL structure is and we’re getting ready to remove the /blog directory since we don’t need it as the new website will be 100% WordPress. For this redirect sequence, we’re going to use the Simple 301 Redirects plugin which will now point to to shorten the URL and make it a more focused URL for Google to see and have more of the article titles display in the SERPs. This is why we’re loving WordPress sites! – Patrick

  37. Jon
    Permalink to comment#

    I currently have a redirect set up as follows

    Redirect 301 /gb/41-fire-refuge-safety-signs

    The above redirect works fine, however when I type in
    I am redirected to
    and want to be redirected to

    Is there a way in which this can be achieved?

    • Neil McLeish
      Permalink to comment#

      Hi Jon.
      I am having a very similar issue. Did you manage to get this solved?
      If so, could you post the solution please, it would be a massive help?

      Many thanks in advance


  38. Doug
    Permalink to comment#

    I have an Ubuntu 12.04 box running 3 sites. One of them I need to force to use vs just I have mod_rewrite is running and I have placed a .htaccess file in my /var/www/site directory with this code in it and restarted apache. Nothing, no effect. I even change the redirect to point at google just trying to see if it was working at all. Nothing. Any suggestions, do I have it in the wrong directory.. etc..

    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^ [NC]
    RewriteRule ^(.*)$$1 [R=301,L]

  39. Alvin
    Permalink to comment#

    How do I redirect all 404 errors in wordpress to my homepage?

    Do I have to do this in htaccess or some other place?

    Tried this code but it is not working :
    RewriteEngine on
    ErrorDocument 404 /index.php

  40. Sam
    Permalink to comment#

    I use the 301 redirect to remove “www” from the domain name. I noticed that css-tricks also have your “www” removed. I wonder if that have an effect on page popularity (link history) for older web pages with “www”.

  41. S M Abu Sayeed
    Permalink to comment#

    How to redirect /~benchmak/catalog.html?page=shop.browse&category_id=62 to ?
    Any help would be appreciated .

    Thanks in advance.

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";