How To Correctly Redirect Your Website

.htaccess 301 Redirect

If you have a standard Linux server, you probably have Apache as the actual Web server software. The other alternatives include Lighttpd (pronounced “Lighty”) and Nginx (pronounced “Engine-X”) but they’re not overly common. If you have a file called “.htaccess” in your “public_html” folder, you know you’ve got Apache running on your server.

.htaccess file 301 Redirects taught on this page include:

  • Redirect a Single Page
  • Redirect an Entire Site
  • Redirect all subpages without redirecting the entire domain
  • Redirect a site to WWW
  • Redirect a site to Non-WWW

This is the easiest and cleanest way to redirect a domain or URL.

Note:

If you end your old site or folder URL with a / it will keep the names and folders that are accessed intact so if you create a redirect from www.website.com/data/ to www.kickasswebsite.com/data/ and a visitor attempts to go to www.website.com/data/oldpage.htm will become www.kickasswebsite.com/data/oldpage.htm. This ability will help you most when you need to transfer a site to a new domain.

Also, I am using FileZilla as my FTP program and these instructions are tailored to it. If you use a different FTP program, the processes may vary.

 

How To Redirect a single page

  • Log in to your web hosting account in your FTP program and navigate to the /public_html folder.
  • Right-Click on the file called “.htaccess”.
  • Choose “View/Edit”.
  • Right at the top, copy in one of the lines below that starts with “Redirect 301″.
  • Change the “/oldpage.html” to the page or folder you want to redirect from.
  • Change the http://www.yoursite.com/newpage.html” to the page or folder you want visitors to go to.
  • Save the document.
  • Close the file editor.
  • Click in the FTP Editor.
  • Click on “Finish editing and delete local file.”

The Code Required:

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

You just created a 301 redirect. Be sure to check your work.

How To Redirect an entire site

  • Log in to your web hosting account in your FTP program and navigate to the /public_html folder.
  • Right-Clisk on the file called “.htaccess”.
  • Choose “View/Edit”.
  • Right at the top, copy in one of the lines below that starts with “Redirect 301″.
  • Change the http://website.com/” to the page or folder you want visitors to go to.
  • Save the document.
  • Close the file editor.
  • Click in the FTP Editor.
  • Click on “Finish editing and delete local file.”

The Code Required:
Redirect 301 / http://website.com/

You just created a 301 redirect. Be sure to check that it works.

Redirect Only Subpages

If you need to 301 redirect all the subpages of a domain but don’t want to redirect www.domain.com itself, you can use the following bit of code.

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

This bit of code was donated by James. Thank you, James.

Redirect to WWW or to non-WWW

Some people want to have the WWW at the beginning of your URL. Other people think it looks grittier not to have it. Whichever you prefer, and thankfully it doesn’t truly matter, is up to you. Doing a redirect from websitecom to wwwwebsitecom is fine as long as it is a correct 301 redirect. It’s also perfectly acceptable to redirect wwwwebsitecom to websitecom.

It’s just best if you do choose 1 way or the other for SEO because Google can view website.com and www.website.com as separate sites. This means that if you have incoming links going to both website.com and www.website.com there’s a chance that they will both build up different pages in Google’s eyes. So to correct that, here’s how you can make everything look uniform using the htaccesss file.

Redirect to WWW with the htaccesss file

To edit the .htaccess file, follow the instructions above and insert the following code at the head of the file.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule (.*) http://www.website.com/$1 [R=301,L]

Redirect to non-WWW with the htaccesss file 

RewriteEngine On
RewriteCond %{HTTP_HOST} !^website.com$
RewriteRule (.*) http://website.com/$1 [R=301,L]

Redirect 404 Errors to home page

If you have multiple pages that come up with a 404 error and you don’t want to take the time to redirect each and every one, I highly recommend entering the following code into your .htaccess file. It’ll automatically redirect all 404 error pages to your home page, thus saving your SEO and keeping people on your site.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]

You just created a 301 redirect. Be sure to check that it works.

66 Responses to .htaccess 301 Redirect

  • Hi, You did a great job on this post. I will definitely digg it and use it to redirect my sire to www.

    • Thank you for the compliment. The .htaccess files is extremely powerful and I hope you can use it to do exactly what you need.

  • Nice tip, I was looking for ways to redirect my website to non www, thanks a bunch!

  • I’ve been looking for high quality instructions on how to use the .htaccess file to do redirects. Searching through Yahoo I stumbled upon your site. Reading this info, I’m pretty sure I’ve discovered just what I needed. I’ll be implementing it immediately.

    • Glad you like it. I’m sure it’ll show you how to do exactly what you need. If you have any questions, feel free to leave another comment.

  • Hi!
    I’m looking for a way to redirect lovespells.pw to http://www.lovespells.pw. because of the google positioning issue.
    I tried the cpanel solution and this htaccess solution, and although both pass the 301 redirect checker, when trying to browse the website I get messages saying that the website has a redirection loop.
    Please, can you help me?
    Thanks a lot!

  • Thanks for the great directions, Tim! I was just wondering . . . I am changing from one site platform to another and I have hundreds of URLs that will be changing. Do I need to list each page individually on the .htaccess file, or is there some easier, more global way of doing the redirects. More specifically, my current site sells downloadable images with each image/product with a URL that looks like this christianimagesource . com/prophet_isaiah_g339-prophet_isaiah__image_1_p2276.html. I am now moving my galleries to WP using WooCommerce and am hoping the URLs will look more like this >http://christianimagesource.com/prophet-isaiah/prophet-isaiah-image-1/ . Am I going to have to list each image page separately on the .htaccess? Is there some easier process? Thanks so much for the help.
    Karen

    • Hi Karen,

      Unfortunately because of how you set up your original site, you will have to list each one individually.

      Here’s why… You didn’t use sub-folders. If you did, you could do something like
      Redirect 301 /prophet_isaiah_g339/ http://www.yoursite.com/prophet_isaiah/
      and you could keep the file names the same. But you created each product with its own page in the public_html directory.

      My best suggestion is to do 1 of either 2 things…
      1) Use the redirection plugin that I talk about at http://www.howto301redirect.com/wordpress-page-301-redirect/.
      2) Create an excel file that can create your pages automatically. This will take a lot of work and will include many get and replace commands but if you do it right, you will be able to export a CSV file that you could then finagle into your .htaccess file.

      Either way, your original file and naming structure will take A LOT of work to transfer to WordPress. Sorry.

      P.S. If you need help, I may be able to help you create the Excel file to expedite your work.

  • Good, clean explanations – really appreciate it. Just a note, “redirect” is misspelled in the header on this page (“r” is missing). Don’t want you to lose SEO from a typo.

    Thanks again for the very helpful post.

  • We had someone do a global 301 redirect on our site gourangaoo4u.com to hippyclothingukatg4u.com but it seems to have undirected. We dont want to have to ask for it to be done again and wondered if you could explain to us how to do it ourselves. We also want to do a global redirect from our other website gourangaoo4u.co.uk but not sure if we do the redirect in the cpanel. Do the search engines recognise the redirects as well as it does if its done via htaccess? Also will we have to do redirects from every single page for optimum SEO or will just the catergory pages suffice. Thanks for any advice

    • Whether you do the redirect in CPanel or in your .htaccess file really makes no difference if you’re doing a site-wide redirect. Either one will work for both usability and SEO purposes.

      If you remember to use the “Make this a wildcard redirect” option in cCPanel, it’ll redirect everything to the same page name page in your new site. That’ll be all you have to do, if you kept the page names the same. If you didn’t, I’d recommend using the “Redirect 404 Errors to home page” directions on the http://www.howto301redirect.com/htaccess-redirect/ page.

      • Hello Tim
        We are experiencing problems, as we have done a 301 irect in the cpanel and some pages redirect, but the images of products where customers order itemsi s still pointing at gourangaoo4u.com and not hippyclothingukatg4u.com. Once on a gourangaoo.com URL it loses any redirects. We are concerned about this as google will see it a duplicate content. We tried the wildcard button but it stilll didnt work. Not sure what Im doing wrong?

        • I would love to check out your problems however both sites come up with a warning from Malwarebytes, my anti-malware software, saying that it has blocked access to a potentially malicious website. I’m not going to risk getting a virus.

          It’s time for you to enlist the help of a professional for 2 reasons:
          1) Your site may be infected with malware.
          2) You need to have these fixed before you get slapped or lose your images.

  • Hi, Thanks for the very informative post! I was looking all over without success for just the information you stated so clearly here. I have not begun to rename any pages for the site I’m working on, but it was originally designed with WebPlus, a god-awful program I’ve never heard of before this, which generates random letter/number strings for both pages and images. I’ve never seen a code mess as bad as this before! So I think the most efficient way to fix it and increase the SEO value of the site is to rewrite the site, implement cleaner css (another nightmare exists here!) and rename all the pages and images. I think you’ve given me all I need to properly redirect the pages once I rename them, but after reading the comments I realize I need to redirect the image urls too and I’m not sure how to handle that one. Can you direct me to a page with instructions or, if you have time, give me a hand here in the comments? Thanks so much!

    • Hannah,

      You don’t need to redirect your images if you do them right. The problem wit the other person’s site seems to be that they didn’t move the images to their new domain but left them on the old one.

      As long as you either:
      A) Move the images with you and link to them properly, or
      B) Don’t change your domain name, leave the images where they are and use the names already given,
      then you’ll have no problems.

      There are larger than normal issues with Kevin’s site that any decent designer won’t have a problem with. Unfortunately he seems to have left his images on the old domain and linked to them there.

      • Hi Tim, thanks for your help with the image “non-redirect.” I renamed them, but they are on the same server and I resubmitted my sitemap with updated mod dates so google would re-read the site with the new image urls. However, I’m having trouble with the page redirects. I tried to use your redirect code and came up with an Internal Server Error/500 error message. I’ve been working to update the outdated code, page names and file structure on my client’s site to make it easier for the search engines to read it, but if they can’t load the site… As the htaccess file was empty before I started, I wrote it just as you said, with the redirect lines starting right at the top. Here is the first line:

        Redirect 301 /page3.html http://www.christinamadden.com/contact.html

        What am I missing? Do I need to turn on the rewrite engine, and if so, how would I write it in this case? I’ve seen plenty of examples for when you want to redirect the entire site, but not when redirecting every page except index.html. In our case we’re not moving, we’ve just completely changed the file organization on the site to include category directories for her paintings as well as renaming all the pages to be more meaningful than “page101.html”.

        Fortunately I uploaded all the new stuff while leaving the old pages in place, so all I had to do was replace the new index.html with the old one and delete the htacess file and the site came right back up. I’d like to upload the new htaccess file and updated index.html as soon as I can Monday – hope you’re not too busy to give me a hand!
        Thanks,
        Hannah

        • Hey again Tim, I kept searching for answers and came full circle to your page – and realized I hadn’t watched your video. sure enough when I played it there was the RewriteEngine on command staring at me from the top of the htaccess file on your screen! Added it to htaccess, reuploaded everything that needed to replace the old stuff and voila! There it is. Thank you so much. It appears that everything’s working fine, but I’m going to check a few urls at your 301 redirect checker to make sure.

  • Thanks, the video helps a lot.

  • This is an extremely informative article. It’s just what I was looking for. I was able to easily redirect 1 folder to another for SEO purposes. My web designer wanted to charge me a lot of money to to it and you saved me $250.00.

    Thank you so much!

  • I can’t find the info for this anywhere! Do any of you guys understand .htaccess redirects enough to know how to fix this:

    I am looking to 301 my old domain to a new domain WITHOUT redirecting the old domain’s homepage.

    I need olddomain.com/page-1 to 301 to newdomain.com/page-1 for all pages etc
    so, everything mimicked from olddomain.com to newdomain.com EXCEPT I don’t want the root olddomain.com forwarding to the root newdomain.com

    hope I haven’t made that sound over complicated. How is this done please!?

    • Settings are inherited: commands in a .htaccess file affect both the directory it is in and any subdirectories of that directory. A file in your main directory will affect your whole site. To override inherited settings in a subdirectory, add a .htaccess file to the subdirectory.

      …so you could leave the old domain’s root file alone, and place a .htaccess file in each of the subdirectories that you wish to redirect :)

  • Hi Tim, thanks for your help, this has really done wonders on one of my site. i am working on another site joomla which have not been able to solve the problem from non www to www http://southernvalleysafaris.com/ i dont know where am going wrong.
    After inserting the code on with the actual name of the site on domain

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

    I am inserting the code after
    # mod_rewrite in use
    RewriteEngine On
    Any help on this

  • Hi Tim, thanks for help and have a nice Christmas!
    I have a special problem. On our new website we merged few pages (we had 4 short – good ranked – pages about our warehousing options, now we have only one page with all 4 options). I think the best to create 4 lines in htaccess file and redirect the old pages to the same new one… will it work? robots will like it?
    Thanks!

    • Arnold, you are exactly right. 4 lines that look like this:
      Redirect 301 /oldpage.html http://www.yoursite.com/newpage.html
      will keep approximately 99% of the link equity and respect that your previous pages had. And, anyone who had those pages bookmarked will now be redirected to the new page.

      It will work. Robots all understand 301 redirects. Go for it.

      And Merry Christmas to you too. :-)

  • Thank you very much! I was banging my head over this for quite some time.
    I use ispconfig and could not figure out how to properly redirect my entire site to http://www.domain.com. Editing the htaccess file worked quickly and easily.
    Thanks again!

  • Hi Tim

    Wonder if you can help. I need to redirect BOTH www and non-www to a new domain.
    I currently have;

    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^www.OLDURL.com.au$ [OR]
    RewriteCond %{HTTP_HOST} ^OLDURL.com.au$
    RewriteRule ^(.*)$ http://www.NEWURL.com/ [R=302,L]

    But only the non-www version is working. Any ideas?

    thank you :-)

  • Hi Michelle!
    You’re making that much more difficult than it has to be. All you need to do is enter 2 lines that looks like this:

    RewriteEngine On
    Redirect 301 / http://www.NewUrl.com/

    That will redirect EVERYTHING to the new domain.

    The exception would be if you want the pages to redirect exactly as they are. That would be easily accomplished using the redirect taught at http://www.howto301redirect.com/redirect-your-domain-in-cpanel/.

  • Hi Tim,
    How to make an old domain show a new domain content, but keeping the old domain appearing on browser url field?
    Example:
    newdomain.com/store must be viewed as olddomain.com/store on browser field and load the newdomain content
    Is it possible??
    Thanks!

  • Hi Tim,

    thank you for your work here!! I managed to redirect my site.com to www. in two secs!! :) :)

    I have this other issue, which I had left for impossible. But I was wondering if I could fix it with one 301 redirects.
    The blog on my site can be reached through 2 different paths:
    amalur-zen.com/component/k2/item/blog-post
    and
    amalur-zen.com/blog/item/blog-post

    Google is hating this and I’d like to redirect all /component/k2/ to /blog/, but no idea how to do it… Is it possible??

    Thank you so much!!

    • The code you need is like this…
      Redirect 301 /component/k2/ http://www.yoursite.com/blog/

      That will redirect everything that is in the /component/k2/ folder to the /blog/ folder. If you have multiple folders that you need to redirect, you may need to create multiple lines in your .htaccess file.

  • Hey Tim,
    Thanks for the wonderful and thorough explanations. I would like to have my website sleeptemperature.com be the mothership website and have many baby websites that point to it. I want the baby sites to have their own content but use the same store as sleeptemperture.com/store so I can just manage one backend. I am on WordPress. The trick is when a customer is on babysite.com/store I do not want it to say sleeptemperature.com/store. How can I fake the URL to use the baby’s URL but really redirect to the mother store?

    Thanks

    • Hi Brad,

      I answered a similar request a few comments earlier. What I told the other poster is this:

      The only way I know of to do that would be using frames, not a redirect. Try something like this… http://create-a-website-with-frames.com/
      Good luck. :-)
      I’m not sure about the SEO implications of having a the entire page within a frame. You should look for that information before continuing.

      P.S. Sorry it took so long to get back to you. My schedule has been a bit wonky lately due to the holidays. :-)

  • Hey Tim- By far the best post on 301′s I’ve found yet. I have been researching extensively and keep finding subtle differences and haven’t been able to make it work yet. I’m hoping you might hook me up too.

    I need to permanently redirect all pages from yogibrown.com to jbrownyoga.com/blog/

    Does this look right to you?

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.yogijbrown\.com$
    RewriteRule (.*) jbrownyoga.com/blog/$1 [R=301,L]
    RewriteCond %{HTTP_HOST} !^yogijbrown\.com$
    RewriteRule (.*) jbrownyoga.com/blog/$1 [R=301,L]

    Much obliged

    • Just to be clear, I need to retain all the subsequent url structure. So…..

      yogijbrown.com
      redirects to
      jbrownyoga.com/blog/

      and

      yogijbrown.com/2013/12/name-of-post
      redirects to
      jbrownyoga.com/blog/2013/12/name-of-post

      Thanks in advance.

      • Mr Brown,

        I think you’re way over thinking it and doing this the hard way. If I were you, I’d use the instructions at http://www.howto301redirect.com/redirect-your-domain-in-cpanel/. Good luck!

        • Hey Tim-
          Thanks for getting back. I just tried these instruction through Cpanel and it only redirects the old homepage to the new homepage. The rest of the urls did not map. I need …
          yogijbrown.com/2013/10/unbearable-sweetness-of-yoga/
          to redirect to
          jbrownyoga.com/blog/2013/10/unbearable-sweetness-of-yoga/
          also.
          Did I miss something? I’m going to leave it messed up for a bit so you can see but will remove the redirect in a bit because I hate to see the posts look so terrible. So appreciate the help. If you’re ever in Brooklyn and want some yoga, I got you covered (as my guest -free of charge.)

          • Glad you got it sorted.

            Thank you for the offer. I don’t get to the great north east very often but next time I’m in Brooklyn, I’ll look you up. In all honesty, I could really use some yoga. I’ve never been able to touch my toes. Heck some mornings I can’t even touch my knees. And that’s a fact.

          • For anyone else who happens upon this, I ended up getting it to work with just one statement in htaccess:
            Redirect permanent / http://newdomain.com/
            Works.
            And for the record, Tim is the kind of person who makes the internet a human place. That he took time out of his life to help a random person sort out there website is a beautiful thing. Big ups.

  • Yep. That’ll do it. In fact, it is toward the top of the post under the heading “How To Redirect an entire site”. I probably should have directed you there instead of sending you to cpanel. Sorry. :-)

  • Hi Tim, excellent article! I’m just wondering if you can help me with the following: I’ve been working on a make over for this site, which is re-launching in a couple of weeks.

    The product urls have been modified for hopefully better seo referencing. I’ll be going from mydomain/index.php?produit=2 to mydomain/index.php?produit=nameofproduct . If I understand correctly, I only have one line of code to write, as opposed to doing one for each product?

    Secondly, a slightly off topic question.. Do you have any recommendations of removing the index.php from the url? Are there any risks doing so? Is it even worth it ? That’s 3 questions I know, any advice would be greatly appreciated.

    Many Thanks. Awesome site!

    • Thomas,

      I can redirect a ton of product pages to the same page in a different folder with 2 easy lines of code but I know of no way to add a different product name to each URL using a single line of code.

      Having examined your website and due to the fact that your site structure is not what I’m used to (I mainly live in the world of WordPress) I am extremely hesitant to make the recommendations that you are requesting. I have never attempted to remove the “index.php” from URLs like yours. The following code may or may not work. If it does, great. If it doesn’t remove it immediately.

      RewriteEngine on
      Redirect 301 /index.php/ http://mydomain.com/

      AND, you should really consider the SEO aspects of your website. It is considered an SEO best practice to make URLs easily readable. mydomain.com/?produit=nameofproduct certainly isn’t as easy for people to read as mydomain.com/nameofproduct.php would be. Google should still be able to index it, but the URL may turn prospective buyers off.

      • Hi Tim, thanks for the reply. The url is this way due to how the database is set up. Essentially, both the old and new site (currently new_index.php?produit=epp800 for example) are built around the database. As our rankings are relatively good, I’m worried about redirecting the product results… as it’s not something I’ve really touched upon before. Losing the index.php? in the url isn’t overly important but something I’m looking to implement in the future after the relaunch.

  • I recently added SSL to my site(https://themydomain.com) Https is working fine . I want to write a redirect rule in .htaccess, When I am trying to access
    http://themydomain.com/signup it wants to redirect to https://themydomain.com/signup.

    2.http://the.mydomain.com/login -> https://the.mydomain.com/login

    3. http://the.mydomain.com/work -> https://the.mydomain.com/work

    I have checked lots of rules, but they didn’t work for me.

    Please help me. It’s Urgent

    • Zam,

      The problem is that somewhere, you already have a redirect going from http to https. If you make an .htaccess redirect from https to http, you’ll create a loop and completely break your website. And I’m pretty sure that’s NOT what you want. ;-)

      As you didn’t include your actual domain for me to check, what you need to do is figure out where it’s doing the current redirect and remove that. I would highly recommend that you contact both your webhost and the company that sold you your security certificate. One of them should be able to help you track down the issue.

  • Hi Tim,
    You have the best info on the subject I have found. Thank you! I have been trying and trying to properly do my 301 Redirects. I even escalated the issue with my host. The problem I have is when I use just “Redirect” it works. However, when I place “Redirect 301″ in the code it gives up a 500 internal server error. My host said if I don’t use 301 in the code it is improper. I’m stumped. Thoughts?

    • Bonnie,
      Currently your redirect is a 302. It isn’t bad because it’s still getting to the right place but it isn’t getting all of the SEO link equity that it could be.
      Can you please copy the redirect portion of your .htaccess file and paste it here for me to look at?

  • Hi Tim-

    Thank you for writing such an informative post on redirecting urls.

    Question: we completley redesigned our website and use WordPress as our CMS. Unfortunately, the developer forgot to redirect all the blog postings /blog to the new site .com/. Needless to say this has been disastrous with almost a 95% drop off in visitors.

    What is the best and most advisable way to redirect, now, (487) posts labeled as 404′s in Webmaster to .com/?

    The *new website redesign* (without having the redirection) has been live since December 2013 and we just caught onto the drop off this past week.

    What a nightmare!

    Thank you in advance-
    Chris