301 Redirect

Redirect Old Domain to New Domain via .htaccess

When you migrate a site from one domain to another, it is very important that you don’t break all the links that you built to your old domain. Proper redirection of all the pages on the old domain to the same location on the new domain will ensure that visitors to the old domain will end up in the right place.  A failure to redirect will result in a loss of visitors as well as search engine rankings.

We are assuming that your web server uses Apache for the purpose of this tutorial.  If you have not made any changes to your overall site structure, but have simply relocated the site in its current state, you can add the following lines to your .htaccess file located at the root of your old domain:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
  RewriteCond %{HTTP_HOST} ^www.olddomain.com$
  RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
</IfModule>

If you have made changes to your site structure, you can still use the lines above on your old domain, but you will also need to create redirects in the .htaccess file on your new domain to handle the specific site changes.

To redirect a single page to a new location on the same domain, use the following syntax:

Redirect 301 /old/old.htm http://www.domain.com/new.htm

It is possible to create rules that will redirect URLs that follow a certain pattern to a new location.  Since these rules involve complex regular expressions, we do not cover them here.  An SEO professional can help you create these more complex and situation-specific rules.  Just be sure that you use a 301 redirect for relocated content.  Any other type of redirect will not preserve your search engine rankings.

NOTE: Due to the sheer number of comments on this post, I’m closing comments at this time.  I’ve noticed that a lot of the same questions are getting asked over and over, so please look through the thread and you may find exactly what you need.

Be Sociable, Share!

Related Posts:

174 thoughts on “Redirect Old Domain to New Domain via .htaccess

  1. I tried above code but its not working as per expected.
    I am testing this on my local machine using WAMPP server.Though when i hit http://localhost/wordpress/ i am getting redirected to http://www.mysite.com/wordpress/ but for other URL i am not getting redirected at all.for e.g

    i have this URL in my local machine http://localhost/wordpress/2010/11/shadows/ and this at the server http://www.mysite.com/wordpress/2010/11/shadows/ but when i hit this URL i am not getting redirected to respected URL on the live server ,but i am being showed same page from local machine

  2. @umesh,

    I am not entirely sure what you have implemented or what you are trying to achieve. Putting the rule on your local .htaccess will only redirect page requests on localhost. Placing the rule on the remote site will only redirect page requests on the remote domain. This rule is designed to forward someone from an old domain that is being phased out to a new domain. So this should just be a one-way rule. If you added the rule on both sites with the domain logic reversed you would end up with an infinite loop of redirects between the two sites.

    I could be entirely misunderstanding your question, so just let me know if I am.

  3. Hi Micah Wood
    We have 2 domains say old.abc.com and http://www.new.com, we are moving to new domain due to some reasons everything else is same like database,permalink structure etc,
    So idea is to hold as much indexing(SEO) as possible so that if user should land at a corresponding page on new domain and in this case we can also inform search engines that we have shifted to new domain.
    I have same setup in my local machine and was tryin to see if the redirection is happening in correct way or not for e.g
    localhost/wordpress/abc/mypost should be redirected to http://www.new.com/wordpress/abc/mypost

    1. This rule should handle redirects from localhost to the remote site:

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

      If you are wanting to only redirect a specific subdomain, you would do something like this:

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

      Please note that the above rules will only work if the Apache Mod-Rewrite moduled is enabled.

      The following is a simplified rule that will handle redirection of any requests on the old domain to the same location on the new domain.

      Redirect 301 (.*)$ http://www.new.com/$1

    2. hi Umesh,

      i own multiple domains for my site MethodShop, including methodshop.com, methodshop.net, etc.. When doing maintenance, i’ll divert traffic between the different sites so my users don’t have their experience interrupted. below is the htaccess wildcard script i use. it takes whatever URL the user attempts to access and mirrors that link on another domain.

      for example,
      `http://methodshop.NET/games/play/duckhunt/index.shtml`

      would redirect to
      `http://methodshop.COM/games/play/duckhunt/index.shtml`

      here’s the one line htaccess script for methodshop.net that rewrites all methodshop.net URLs to methodshop.com. just edit it for your domain.

      RewriteEngine on
      RewriteRule (.*)$ http://www.methodshop.com\/$1 [R=301,L]

      1. Not a bad idea being able to switch between two sites while doing maintenance. I assume you comment out your one-liner when you aren’t in maintenance mode. You might want to rethink the 301 redirect though. If you are just doing site maintenance, you don’t want to signal a permanent redirect to search engines. It is best to use a 302 (temporary redirect) instead.

  4. Thanks for the clarifications!!
    This means if i want the redirect from old site to new site all i have to do is

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

    And i don’t need to map individual URL to new URL until unless the new URL structure does not match with the new one?

  5. @umesh,

    Exactly. Of course the rule you referenced would only redirect the ‘old’ subdomain, not the entire ‘abc.com’ site. If you wanted to redirect the entire site, you would want to make sure you catch all www and non-www traffic like so:

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

    If you additionally wanted to redirect the ‘old’ subdomain to a specific directory on the new site, you would do something like this below the previous code:

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

    Then, lets say you have http://www.abc.com/my-page on the old site. The user would be redirected to http://www.new.com/my-page on the new site. However, if you have changed the URL on the new site from ‘my-page’ to ‘your-page’, then you would want to do a one-off redirect in the new site’s .htaccess for that page, like so:

    Redirect 301 /my-page http://www.new.com/your-page

  6. i have a query Pl, help me regarding it
    We have single domain http://www.newdomain.com, we re-name the few pages name for seo pointofview, but someone or search engine crawler record old page name has been stored

    i have to add some like redirect code when any one visit old page name, its redirect to index page
    So, pl help me.

    1. No problem. For each page you renamed, just do a 301 redirect like this:

      Redirect 301 /old-page http://www.mydomain.com/new-page

      It is ideal to redirect the old page to the new one, not to the homepage. If a page is deleted and there is no equivalent, then you would redirect to the homepage.

    1. @huunam09,

      I am not sure I am following entirely. It seems that your website is at domain1.com and you are wanting to link to your web store, which is hosted by Shopify. If you setup Shopify to use a custom domain, or in your case ‘domain2.com’, then that will be the method by which users will visit your store. Basically, what would happen is that you would point ‘domain2.com’ to the Shopify web server. You would then just use ‘domain2.com’ as the link from your main site to the web store hosted by Shopify.

      To setup your custom domain with Shopify you don’t need a redirect at all. You just need to update your domain’s DNS entry with your domain registrar to point the domain to the Shopify server. After you update your DNS, it will take some time to take effect… so don’t change it back right away thinking it doesn’t work.

  7. Micah
    I am trying to set 301 redirect from my old domain to new domain and here is what i have done in my .htaccess file

    RewriteEngine On
    # redirect the changed URLs individually
    RewriteCond %{HTTP_HOST} ^old.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.old.com$
    RewriteRule ^take-the-tour$ http://www.new.com/archive/ [R=301,L]

    RewriteCond %{HTTP_HOST} ^old.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.old.com$
    RewriteRule ^submit$ http://www.new.com/guest-post-opportunities/ [R=301,L]

    ……..some more one-to one mapping…..

    # Finally, redirect everything else as-is
    RewriteCond %{HTTP_HOST} ^old.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.old.com/$
    RewriteRule (.*)$ http://www.new.com/$1 [R=301,L]

    # BEGIN WordPress
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    but its not redirecting at all and old domain is still landing at old site.i tested it on my local machine as i have indicated at other comment and everything was working fine.
    Not sure what going wrong here

    1. @umesh,

      Any reason you wouldn’t just use the simple syntax for your single page redirects?

      Redirect 301 /take-the-tour http://www.new.com/archive/
      Redirect 301 /submit http://www.new.com/guest-post-opportunities/

      1. so i do not need to worry about domain with or without www? more over i am surprized even simple redirection is not working like if i remove one-to one page mapping even than this should work

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

        but unfortunately this is also not working

  8. Please help. First of all, please understand that we are more than beginners or novices in .htaccess mod rewrite stuff. So, having said that, PLEASE HELP!
    Most pages have duplicate content by having http://www.example.com and http://www.example.com/index.html. How do you get rid off that second one http://www.example.com/index.html but still keep the same website structure?

    Also, we just changed our website – new domain name, new folder names but no subdomains and forwarded all the old webpages (folders) using RewriteRule ^oldwebpagename.html$ http://www.example.com/ [L,R=301]. Great, right? NO! We cannot forward the last home page http://www.example.com. We forwarded http://www.example.com/index.html but not http://www.example.com. How do we do this?

    PS. We had about 25 old pages that we wanted to link directly to new pages that had long names, good for SEO; for example, from http://www.example.com/bowls to http://www.mynewdomain.com/plastic-bowls-for-dogs. Thank you.

    HTML website, Apache server, .htaccess enabled.

    1. First, redirecting from http://www.example.com/index.html to http://www.example.com will cause an infinite loop since these are the same page. For SEO, I recommend simply using canonical link. If you are trying to forward these to the same page on a new site, don’t worry about the structure.

      Second, it sounds like you are redirecting individual pages and are unsure how to redirect only the homepage. If you want to redirect just the homepage, try something like this in your old site’s .htaccess file:

      RewriteEngine on
      RewriteRule ^$ http://www.example.com/ [R=301,L]

      1. Thanks Micah for your prompt reply. I want to redirect the home page as well as all subpages. Those subpages will be linked directly to the new website’s pages, not to the root folder. How can I achieve that? Thank you.

      2. Hi again! How would I redirect all OLD images that have been indexed by Google to the new site? Remember, that the new site does not have the same path or image names anymore. So, how would I properly redirect images, either the entire folder called “images” from the old site to the new (they could even go to the main webpage http://www.example.com – if that is possible) or to individual image location on the new site:

        http://www.myoldsite.com/images/banner-main.jpg referred to either:

        http://www.mynewsite.com
        or
        http://www.mynewsite.com/files/images
        or
        http://www.mynewsite.com/files/images/newpage-banner.jpg

        What is the best solution with regards to best SEO practices. Thank you in advance.

        Sebastian

        1. Ideally, when migrating a site you don’t want to change the structure until you have given the 301’s some time. This makes it really easy to make sure everything redirects properly. Obviously you aren’t in that situation. You can always do single redirects for every page, image or file for which you want to do a 301. Any 301s that you do should go to the most similar resource on your new site. So this is probably the best redirect given your example:

          Redirect 301 /images/banner-main.jpg http://www.mynewsite.com/files/images/newpage-banner.jpg

  9. Hi Micah,

    I keep running across your excellent work and resources all over the web… yer awesome!

    Here’s what I’m facing: One of my clients has a website that she is splitting into two. The old domain will no longer be used, so everything needs to be redirected to the new sites. Some of the content is going to one domain and some is going to the other. All of the content will be in different folders from the originals on the new domains as well…

    I’m in over my head here! Is there anyway to redirect to two different domains from one domain?

    Thanks!
    c

    1. Celeste,

      The situation you describe is never easy. The best thing to do is to create a spreadsheet where you map old urls to new ones. In some cases you may find that you can redirect entire directories, and in other cases you will need to redirect individual pages. It is definitely possible to redirect from the old domain to the two new ones. However, especially since the directory structures are changing, you will have to be much more granular in your redirection of content.

  10. Hi Micah,

    Thanks for your reply!

    I would also like to create a catchall to deal with any pages that don’t make it to either of the new sites. Will something like this work, or will the first line negate the rest of the more specific lines?


    Redirect 301 / http://www.new-domain1.com/

    Redirect 301 /old/old1.htm http://www.new-domain1.com/new1.htm

    Redirect 301 /old/old2.htm http://www.new-domain2.com/new2.htm

    Thanks for your help!!
    c

    1. I believe you would want to move your catchall rule to the bottom. Start with your more specific rules first and then work your way to the more general rules.

  11. hi Sir,

    thanks for your help i need some help i want to know i want to redirect my old domain to the new domain but i dont want to change the subfolder i mean like this

    http://www.olddomain.com/the_king.html
    to
    http://www.newddomain.com/the_king.html

    i mean when the user visit this link
    http://www.olddomain.com/the_king.html its automatic redirtect to the new domain with the same location like this
    http://www.newddomain.com/the_king.html

    can you tell me this is possible because i want to do this i shall be very thanksful to you for this thanks once again !

    1. Sunny,

      It is indeed possible. The redirect listed in the beginning of this article will do exactly that:

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

      If you only want to redirect traffic from the www subdomain and that’s it, just remove that second line.

  12. Thanks Micah Wood for your reply and answer my english is not too strong but one thing i want to ask you if you dont mind so :( if i use this coding in .htaccess

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

    so it means all of my traffic from old domain will redirect to the new domain with same the link as i already asked you like this

    http://www.olddomain.com/softwares/brrip/idm6.1/

    and i want if any any user visit this link

    http://www.olddomain.com/softwares/brrip/idm6.1/

    its automatic redirect to this new domain like this

    http://www.newdomain.com/softwares/brrip/idm6.1/

    and i have more than 15000 link :( so can you tell me how to do this and one more thing and i dont use subdomain . and which second line i have to removed and i hope u understand what i mean and one thing i have saw in cpanel what are wild card redirects here is the snapshot for that

    http://s10.postimage.org/kv7yboot5/image.jpg

    thanks i will wait for your reply :( thanks mate u are really a great one thanks once again .

    1. Sunny,

      Given your situation, you probably would need something like this:

      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{HTTP_HOST} olddomain.com$
      RewriteRule (.*)$ http://newdomain.com/$1 [R=301,L]
      </IfModule>

      You mentioned that you don’t use the www subdomain, so I took that out. I wrapped the code so that it only runs when the Apache mod_rewrite module is active.

      So what this code does is it intercepts all requests on ‘olddomain.com’ and forwards them to ‘newdomain.com’. Anything a user types after ‘olddomain.com’ is preserved when the user is redirected. Here are a few examples:

  13. thanks and really much appreciate thanks once again and but if i add the second time so it means if the user type www or not hw will going to redirect to the new domain thanks thanks thanks and once again u are really going a Great job thanks and what about the cpanel wild card option

    1. Yes in the code I gave you, if the user does or doesn’t type in www, then the redirect will always go to the new domain without the www.

      The cPanel wildcard redirect should do the exact same thing.

  14. Hi Sir,

    i have replace this code in .htaccess but not working only redirecting the domain to another domain but not redirecting the sub links like htis

    oldomain.com is redirecting to newdomain

    but when oldomain.com/links/something is not redirecting oldomain.com/links/something i want to redirect this link oldomain.com/links/something to newdomain.com/links/something

    so kindly tell me out thanks

  15. Hey mate thanks its start working :( and if we do not replace this coding in the .htaccess but we can use the cpanel redirecting option too thanks Mate u are reallt Great one thanks

  16. I cloned a site to give it a new domain name. I want every page and post link to be redirected to the corresponding page/post on the new domain. I will try the code you give in your article. One thing I don’t understand is do I REPLACE the code in the htaccess file there now, or just add it at the bottom. I have this now:

    # BEGIN WordPress

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPress
    RewriteCond %{HTTP_HOST} ^budgetinteriordesignideas\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.budgetinteriordesignideas\.com$
    RewriteRule ^/?$ “http\:\/\/www\.athomewithjo\.com\/” [R=301,L]

    I did a redirect from my cPanel but it is only working for the homepage. As you can see I already have some of the same code as in your fix. So can you please explain..do I replace any of it, all of it, or just add the new code below. Thanks!

    1. If you are not going to be using the old domain and definitely want to redirect all traffic to the new site, you can replace the contents of your .htaccess file or add to the top of the file. The ‘$1′ at the end of the RewriteRule is important and is what ensures that each individual page on the old site is redirected to the appropriate page on the new site. Without it, everything will be redirected to the homepage of the new site. Also, the url in the RewriteRule isn’t a regular expression, so escaping things on that line will probably break things. Assuming the lines at the end of your example are what you are trying to use, adjust like this:


      RewriteCond %{HTTP_HOST} ^budgetinteriordesignideas\.com$ [OR]
      RewriteCond %{HTTP_HOST} ^www\.budgetinteriordesignideas\.com$
      RewriteRule (.*)$ 'http://www.athomewithjo.com/$1' [R=301,L]

      1. Thank you for this incredibly fast reply! So let me be sure I understand. I would replace the last 3 lines in the code I already see in my htaccess file and sent you in this post, with the 3 lines of code you answered me with?

        Would I add at the top the code you have in your article above, or just leave what I have as is and adjust the last 3 lines. I am very new at this and I don’t what to break the redirects. Thanks!

        1. You definitely want to adjust the three lines on the bottom of your previous example and then move them to the top of the file. You want to be sure that the redirects are the first thing that happens. If that is the only thing that needs to happen, you can just use those three lines as the entire file as well.

          1. Micah…you are my new best friend!! It worked perfectly and your instructions and speed of reply were perfect! I was clueless. Your link was given to me by Host Gator tech support, who himself was having a hard time helping me get exact info. 10 thumbs to you for up to creating one of the most useful sites on the net.
            Thanks a gazillion!

          2. Question plz.. I kinda got the same thing as this woman you helped here.
            For all pages, posts and media.
            But the home page I want redirected to a different site. What should my htaccess file look like? I need all links to be permanent redirects.

            For instance:
            Old site home page to new site A
            The complete rest of the site to new site B
            (All this content has the same url aside from the domain name.)

            Thanks so much, Hope you can help.

          3. Joey,

            Something like this should do the trick:


            RewriteEngine On
            # Send the homepage traffic to Site A
            RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
            RewriteRule ^(.*)index\.php$ http://www.siteA.com [R=301,L]
            # Send the rest of the traffic to Site B
            RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
            RewriteCond %{HTTP_HOST} ^www.olddomain.com$
            RewriteRule (.*)$ http://www.siteB.com/$1 [R=301,L]

      2. Hi Micah,
        I have about 5 sites I am going to move. I copied what I did that worked so well with the site we are discussing here, and now it is not redirecting.
        Here is my old htaccess file.

        # BEGIN WordPress

        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.php$ – [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]

        # END WordPress

        And the site I am moving has a squeeze page on the root and the actual WP site is in a /articles sub directory. I tried redirecting with both paths and no luck.
        Here is my new htaccess file for this site.

        # BEGIN WordPress

        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^clutterfreehome\.net/$ [OR]
        RewriteCond %{HTTP_HOST} ^www\.clutterfreehome\.net/$
        RewriteRule (.*)$ ‘http://www.tidyhometidymind.com/$1′ [R=301,L]

        # END WordPress

        I also tried using:
        ^clutterfreehome\.net/articles/$ [OR]
        RewriteCond %{HTTP_HOST} ^www\.clutterfreehome\.net/articles/$

        I replaced the entire file, just as I did with the first one that worked.What am I doing wrong? Thanks!

        1. Carolyn,

          It appears that the trailing slashes at the end of your ‘RewriteCond’ lines is most likely the problem. Also, the url on your last line probably shouldn’t be quoted. This should do the trick:

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

          1. Hi Micah,
            I discovered that the redirects for this site had stopped. So I just checked the htaccess file and made a seemingly serendipitous mistake. I looked at the htaccess file on the new site, thinking I was on the old site. I pasted the edited file in and saved. Then the redirects properly resumed. At that point I realized what I had done. So I checked the htaccess file on the old site, and it was still just as I had written it before. To be safe I pasted the edited htaccess file code in and saved it. All appears to be working as it should Was this just dumb luck? Should I not have done that to the htaccess file on the new site, the one I am redirecting to?

          2. Most likely what happened is that your generic domain redirect on the old site was working and when you added your custom redirects on the new domain it handled those redirects on the new site. Essentially resulting in two 301 redirects, which isn’t really a problem. However, when you updated the .htaccess file on the old server, you probably fixed the issue and now only one 301 redirect takes place.

          3. Hi Micah,
            Now it is not working, so I went to the new site and deleted the redirect code. Then I have checked and double checked the htaccess code on the original (old) site and it looks exactly what you gave me earlier, which I just pasted in.

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

            Here is the file saved as htaccess on the new site.

            # BEGIN WordPress
            RewriteCond %{HTTP_HOST} ^clutterfreehome\.net/$ [OR]
            RewriteCond %{HTTP_HOST} ^www\.clutterfreehome\.net/$
            RewriteRule (.*)$ ‘http://www.tidyhometidymind.com/$1′ [R=301,L]

            RewriteEngine On
            RewriteBase /
            RewriteRule ^index\.php$ – [L]
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule . /index.php [L]

            # END WordPress
            Sorry for being such a dweeb, but I am clueless about this without your help.
            What is odd is the root, clutterfreehome.net without the /articles where all my content is, redirects to the new site fine.

          4. So if you have WordPress installed in the articles/ directory and the redirect rules on the old site are below the normal WordPress .htaccess block, then it would behave the way you describe. I’m not sure what all is in your old domains .htaccess file. Perhaps you should paste the entire contents of your .htaccess on the old domain.

            The redirect from clutter free to tidy mind isn’t really doing anything on the new domain and can be removed.

            As a side note, there are some things to be aware of when working with 301 redirects.

          5. Hi Micah,
            I am stumped. I read your replies and went back through this entire thread and I simply cannot find the error. I understand about clearing cache with 301′ and I use K-meleon only for checking and I clear the entire cache every time.
            This is the entire contents of my htaccess file on the old site.

            # BEGIN WordPress
            RewriteEngine On
            RewriteCond %{HTTP_HOST} ^clutterfreehome\.net$ [OR]
            RewriteCond %{HTTP_HOST} ^www\.clutterfreehome\.net$
            RewriteRule (.*)$ http://www.tidyhometidymind.com/$1 [R=301,L]

            # END WordPress

            This is the entire contents of the htaccess file on the new site.
            # BEGIN WordPress
            RewriteCond %{HTTP_HOST} ^clutterfreehome\.net/$ [OR]
            RewriteCond %{HTTP_HOST} ^www\.clutterfreehome\.net/$
            RewriteRule (.*)$ ‘http://www.tidyhometidymind.com/$1′ [R=301,L]

            RewriteEngine On
            RewriteBase /
            RewriteRule ^index\.php$ – [L]
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule . /index.php [L]

            # END WordPress

            When I tried removing the redirect code I got a 501 error.
            I can only redirect from the root directory of the old site and not anything from the /articles directory where all the content is. I did some research on htaccess files but it is still beyond my expertise. Shouldn’t there be more code in the old htaccess file..something about iFmodule?

          6. I fixed it!!! I was poking around and discovered the /articles directory had its own htaccess file. I put the code into the file and it is working so far.
            So for nor, problem solved. Thanks for being so patient.

  17. Hi,

    All very new to this, can I just confirm as I am being told that things have changed with Google this year and that not all advise is relevant anymore. Can you still redirect traffic from a .co.uk site to a .com site without losing any rankings or traffic by using 301 redirects and it is easy for a professional to do?

    Many thanks,
    T

    1. Trudy, 301 redirects are still the best way to relocate a site from one domain to another. In most cases, things should go smoothly and you shouldn’t see any loss of rankings or traffic. However, there are some things you can do to test the water before you jump into the deep end. Take a look at this video from Matt Cutts at Google about moving to a new domain.

  18. Hi Micah,

    I just did a copy paste of your code with old and new domain names and it just worked. Thanks for a wonderful post.

  19. Hi Micah,
    Hoping you can help me out.

    I want to redirect from old page http://www.mysite.com/content/view/3/4 to its new page http://www.mysite.com/a-better-tomorrow.html The old site was built using joomla and the new site built using dreamweaver and html. I have put this in the ht access file but it is not redirecting and is bringing up an internal server error.

    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^mysite.com
    RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
    # 301 Permanent Redirects

    Redirect 301 /content/view/3/4 http://www.mysite.com/a-better-tomorrow.html

    Is there something else I should be doing. i have set up similar htaccess files for other sites on the server that work perfectly so am assuming it is apache enabled.

    any help would be appreciated

    Pete

    1. Pete,

      You only need one line for the single redirect:

      Redirect 301 /content/view/3/4 http://www.mysite.com/a-better-tomorrow.html

      The internal server error is likely due a syntax error in your .htaccess file. The easiest way to troubleshoot this is to remove everything out of your .htaccess file and then add things back in one at a time until you see the error again. As always, make sure you backup your .htaccess before making changes.

      1. Thanks for that… I did what you said and yes it was a syntax error. I’m blushing as it was very obvious once I found it…

        Much appreciated…

        Pete

  20. i want to redirect localhost/1-home to localhost/index.php

    so how can i do in htaccess.
    please help me out.

    i tried with
    ===========================================================
    RewriteEngine on

    RewriteCond %{HTTP_HOST} ^localhost/1-home$
    RewriteRule (.*)$ http://localhost/freshoaks/$1 [R=301,L]
    ===========================================================

    1. krian,

      The HTTP_HOST is the server variable which will only have the ‘localhost’ value in it. Since you aren’t working with different domains, try something like this:

      RewriteEngine On
      RewriteBase /
      RewriteRule ^1-home(.*)$ http://localhost/freshoaks$1 [R=301,L]

      That is a rewrite of your code, which appears as though you are trying to redirect everything from the ‘1-home’ directory to the ‘freshoaks’ directory. The exact same thing could be accomplished with this:

      Redirect 301 /1-home http://localhost/freshoaks

      Of course, if you are wanting to redirect everything to http://localhost/index.php you would do this:

      Redirect 301 /1-home http://localhost/index.php

  21. Hi
    I need little help.
    in my public_html folder I have two folders each one for domain I own.
    Folder/domain site1.com inside is only index.html
    Folder/domain site2.co.uk inside is entire shop/gallery.
    What I need is, if somebody type the address site1.com in browser I want to take him right away to the shop/gallery under site2.co.uk

    Thanks

    1. Are you using cPanel? You probably just need to configure an add on domain and point it to the right directory. If you do need to do a redirect, can you provide an example of what you want to have happen with full URLs? I’m not sure I follow what you are trying to do entirely. It sounds like you want to redirect all of site1.com to site2.co.uk, but it isn’t entirely clear why or whether site1.com is ever supposed to load its own content.

  22. I have redirected my website to new domain since july 2012. That time I loosed my ranking and I found some errors in website redirections then I fixed it and I got unexpected ranking results to all my keywords. The most competitive and all keywords came on google first page (top 3). I have a one question about redirection that time I redirected my home page to new domain’s relevant inner pages. Is it right?

    Now, few days earlier when penguin updates come out my keywords ranking go down. We always make quality link related to website theme. I saw my website’s Meta title and brand name also not recognize in google. It’s not showing relevant results. What’s problem in my website? What should I do?

    Here are redirection code that I used at that time

    RewriteEngine on

    # Expire images header
    ExpiresActive On
    ExpiresDefault A0
    ExpiresByType image/gif A300
    ExpiresByType image/png A300
    ExpiresByType image/jpg A300
    ExpiresByType text/css A300
    ExpiresByType text/javascript A300

    ### re-direct index.html to root / ###
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
    RewriteRule ^(.*)index\.html$ /$1 [R=301,L]
    ### re-direct index.html to root / ###
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
    RewriteRule ^(.*)index\.php$ /$1 [R=301,L]

    #ErrorDocument 404 /error-404.html

    ErrorDocument 404 http://www.new.com/

    ### shows links with www / ###
    RewriteCond %{HTTP_HOST} !^www.old.com$
    RewriteRule ^(.*)$ http://www.old./$1 [R=301]

    RewriteCond %{HTTP_HOST} ^www.old.com$
    RewriteRule ^$ http://www.new.com/internal page/ [R=301]
    RewriteRule ^services/abc.html$ http://www.new.com/ [R=301,L]

    1. For starters, why are you using your homepage as your 404 page? You will still be returning a 404 status code, so there is no SEO benefit. Additionally, a well done custom 404 template can be much more helpful to users that your homepage.

      I’m not sure how much of this is typos you might have made on this site as opposed to in your actual code, but I do see some serious typos. Your second to last block is redirecting http://old.com to http://www.old./. You seem to have left off the .com bit. Your last code block looks fine, except for the fact that you have a space in ‘internal page’. Hopefully, your actual pages have slugs like ‘internal-page’ instead.

  23. Hi Micah,

    I need your help i want to redirect old domain to new domain .i want to redirect my old website inner pages to new website inner page. New domain and old domain internal pages names are different and both web servers are also different. i need code which i put in htaccess to redirect home and inner pages of old website to new domain home pages and inner pages….Please help me

    1. @joly – It sounds like you are going to have to do a bunch of redirections. I take it you relocated the site and restructured it all in one step. Ideally, it is best to move a site and do the 301 redirects first. After some time, then you can start restructuring the site and avoid the hassle of a bunch of one-off redirects. Regardless, there isn’t much you can do about it now, but I just wanted to mention this in case it helps someone else or you in the future.

      So when you have to do a bunch of one-off redirects, the best thing you can do is start a spreadsheet to make sure you capture a list of all the old urls and the new urls that they should be mapped to. Reordering this spreadsheet once you have captured all the data will help you identify patterns that might save you some time. Anytime you can identify a pattern, then you can usually write a single rule that can handle any redirects that match that pattern.

      When you have a single page that you are trying to redirect to another single page on a different site, this is the simplest syntax you can use:

      Redirect 301 /coolstuff/old.htm http://www.newdomain.com/freaking-awesome/new.htm

      This will redirect http://www.olddomain.com/coolstuff/old.htm to http://www.newdomain.com/freaking-awesome/new.htm when this rule is placed in the .htaccess file on the olddomain.com site. Notice how we don’t need to enter the domain for the site that rule is hosted on. If you have a bunch of one-off redirects, just copy this line for every redirect in your spreadsheet.

      So what happens when you identify a pattern? Let’s look at a simple example that you are likely to run into. Let’s say that you have http://www.olddomain.com/coolstuff/. This is a directory that contains a hundred different files within it. That directory wasn’t changed when you moved things over to the new site, but it is in a different place. Let’s say the new location is http://www.newdomain.com/cool/stuff/. Obviously a different place, but still all the same cool files you had before. To redirect the hundred files in that directory all at once, just do this:

      Redirect 301 /coolstuff http://www.newdomain.com/cool/stuff

      Pretty simple, but now you have saved yourself a whole lot of time and typing.

      An important side note, these rules are executed in order. If you have a rule that is more specific, always put it first. For example, lets say we combine our two examples from above. The general redirect will send everything from http://www.olddomain.com/coolstuff to http://wwww.newdomain.com/cool/stuff. However, the more specific rule and only exception is that we want the http://www.olddomain.com/coolstuff/old.htm file to actually go to http://www.newdomain.com/freaking-awesome/new.htm. In order for both rules to work as expected, we have to make sure they are in this order:

      Redirect 301 /coolstuff/old.htm http://www.newdomain.com/freaking-awesome/new.htm
      Redirect 301 /coolstuff http://www.newdomain.com/cool/stuff

      If we reversed the order on this, then the general rule would be hit first and our more specific rule would never run.

      I hope this clears things up. Let us know how it goes!

  24. Thanks Micah,

    But website is not too big its about 40 pages…and already old domain have too much traffic and backlinks thats why i want to move complete to new domain..is it good if i can redirected any of 1 page firstly.If that page will rediretc correctly and i get my ranking on same position after that i will move my complete site to new domain

    Let me know

    1. @joly, Matt Cutts from Google recommends that you redirect a small section of the site first and see how your rankings and traffic fare before redirecting the entire site. So on that front, you are doing the right thing. You just want to be sure that you can attribute potential issues to either the moving from one domain to another or the changing of your actual content. Many people make the mistake of doing both at the same time and then never know which one was the cause if they have issues.

  25. @micah:- okay thanks …. So i have decide that i will redirect 1 page firstly after that i will redirect my whole site..So which type of redirection i can use for first page 301 or 302 ? Please provide me code also

    1. @joly, You definitely want to use a 301 redirect. A 301 redirect is a permanent redirect, whereas a 302 redirect is a temporary redirect. A 301 is the only redirect that search engines will recognize and pass any SEO value from one site to another. There are some code examples in my previous response. You should be able to adapt them pretty easily, but if you want to paste some sample URLs and your best guess on the correct code, I don’t mind verifying.

    1. So the first block of code is making sure that all non-www pages on your site are redirected to the www version. The last line, which is your one-off redirect, will redirect from http://www.mysite.com/content/view/3/4 to http://www.mysite.com/a-better-tomorrow.html. I know you replaced your actual domain names with ‘mysite.com’, however, just be sure that you use the new domain in the place of the ‘www.mysite.com’ in the one-off rule.

      Here is the updated code that I would recommend:

      Options +FollowSymlinks
      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^mysite.com$
      RewriteRule (.*)$ http://www.mysite.com/$1 [R=301,L]
      </IfModule>
      Redirect 301 /content/view/3/4 http://www.mynewsite.com/a-better-tomorrow.html

  26. Thanks micah…if i need more help i will write you….is it possible you can mail me your official id for further doubts.

  27. Hey Micah quick question – hoping you could help since you seem like an htaccess rockstar :)

    I am helping someone with a site and here is the scenario:
    They had an old site, had a new site created in a different hosting environment along with a new url. Pointed old url to new url via domain registrar (godaddy). Here is the issue – there are many urls still indexed from the old site that if clicked on go nowhere of course. I have made a list of these old site urls and corresponded them to a url on the new domain/site. I added them to the .htaccess as for example Redirect 301 http://www.oldurl.com/oldpage.htm http://www.newurl.com/newpage.html

    None of them work. Any ideas?

    Also – above all these redirected urls that don’t seem to work is the following:

    Options +FollowSymLinks
    RewriteEngine on

    RewriteCond %{HTTP_HOST} ^newurl.com
    RewriteRule (.*) http://www.newurl.com/$1 [R=301,L]
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
    RewriteRule ^index\.html$ http://www.newurl.com/ [R=301,L]
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.htm\ HTTP/
    RewriteRule ^index\.htm$ http://www.newurl.com/ [R=301,L]

    Thanks in advance for any help!

    Jason

    1. Jason, so there are three reasons that your one off redirects aren’t working:

      1) If you are redirecting via the domain registrar, that is likely taking place at a level above your .htaccess. I would just disable this so you can have more control over your redirects.
      2) The initial block of code in your .htaccess file is running before your specific one-off rules are ever hit. In order for things to work properly, always run your most specific rules first. If they don’t apply, the generic rules will take over from there.
      3) You don’t really define the full url for the old page in your one-off redirects. You only need the path relative to the homepage.

      As such, here is how I would restructure your .htaccess file ( untested of course ):

      Options +FollowSymLinks

      <IfModule mod_alias.c>
      Redirect 301 /oldpage.htm http://www.newurl.com/newpage.html
      Redirect 301 /another-old-page.htm http://www.newurl.com/another-new-page.html
      Redirect 301 /old-directory http://www.newurl.com/new-directory
      </IfModule>

      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html?\ HTTP/
      RewriteRule ^index\.html$ http://www.newurl.com/ [R=301,L]
      RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
      RewriteCond %{HTTP_HOST} ^www.olddomain.com$
      RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
      </IfModule>

      Notice how the most specific rules are always first. If you disable the redirects with your domain registrar and implement these changes, it should work. If not, it could be possible that either the Apache mod_rewrite or mod_alias modules aren’t enabled… though this is highly unlikely.

  28. Hi guys,

    I have setup a few redirects for my old site to go to my new site. The old URL’s from a wordpress install are redirecting to the new Domain’s Address, but appending the old URL onto the end of the new domain. Any idea’s, I hope that makes sense.

  29. Hi guys,

    I hope that someone can help…

    Basically I have decided to change my portfolio url (I am experienced with 301’s but cant think what ive done wrong).

    My old site is: http://www.swex.co.uk which did have a blog on it (/blog/). Google has indexed all of the old posts, categories , tags etc on it. I have a .htaccess with my 301’s in it (ModRewrite is on) to divert the old pages to the root of my new site: http://www.mathewporter.co.uk apart from a few relevant URL’s that redirect (or I want to, but they dont work) to new URL’s for the post’s.

    Anyway if you didnt follow that, dont worry, here is my issue. 301’s from the old URL go to the new site, but the old URL is appended to the new domain as described in your article… I dont want this, I want the majority to redirect to the home page and a select few old URL’s to redirect to the relevant post’s that have moved, but have new URL’s.

    Hope that make’s sense / isn’t to confusing and that someone can help.

    Many thanks.

    1. Mathew,

      Here is how to get your URL’s to redirect directly to the homepage on your new site:


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

      The only difference here is that I removed the $1 from the end of the URL on the RewriteRule line. You can compare the code above to the first code block mentioned in this article. Assuming you replace your actual domains, my original code block would perform the following redirection for a sample URL:

      http://www.swex.co.uk/blog/article/ –> http://www.mathewporter.co.uk/blog/article/

      The new code block above, without the $1, will result in all URLs forwarding directly to the homepage:

      http://www.swex.co.uk/blog/article/ –> http://www.mathewporter.co.uk/

    1. Those redirects aren’t actually using the mod_rewrite module, but will work just fine. However, note that the rules take place in order… so be sure your most specific rules come first or they will never get hit.

      1. I was using mod_rewrite for the old domains home page that was still active and trying to redirect old blog pages that had been indexed which were going to the new site… The new site was using mod-rewrite and appending the url’s to pages that did not exsist, sorted it now anyway.

        Thanks guys.

    1. You can use this code:

      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^sample.com$
      RewriteRule (.*)$ http://www.sample.com/$1 [R=301,L]
      </IfModule>

      This will redirect any page where the www is not present to the www version. Whether or not the http:// part is typed is not a concern. The browser will automatically add this if a user doesn’t type it.

  30. We have acquired a company into ours. Lets call it compx.ca. Our site would be mycomp.ca. They will no longer be hosting compx.ca. I want to have all of compx.ca to redirect to the homepage of mycomp.ca. I made an .htaccess file with:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^compx\.ca$ [NC]
    RewriteCond %{HTTP_HOST} ^www\.compx\.ca$ [NC]
    RewriteRule ^(.*)$ http://www.mycomp.ca/ [R=301,L]

    So even if someone went to compx.ca/about I want it to go to mycomp.ca homepage.
    Would I also need to call the compx.ca hosting provider and have them point compx.ca to the ip of mycomp.ca?

    1. Justin,

      Your .htaccess file looks correct. However, if you have the hosting provider point the old domain to the new one, your .htaccess file will never be read and your hosting provider might not implement a 301 redirect. For SEO, you definitely want a 301 redirect. I would just handle the redirects via .htaccess for now so that you have more granular control, just in case you need it. If possible, it is ideal to have the pages on the old site redirect to the closes match on the new site.

  31. For some reason this is just to confusing for a newbie like me.

    My company has a new website on this adress.
    http://www.vision2watch.nl
    and i need our .com to show it as well.

    But it keeps leading to the wrong page instead of the homepage.

    Can you be so kind and help me with the code i need to fix this?

    1. Sorry, I am going to need more details / examples of what is going wrong. Are you trying to use both domains or do you want one to be your primary domain for SEO purposes?

  32. Hell,
    I am trying to redirect non www url to www url using .htaccess,but can’t redirect. Code –
    # BEGIN WordPress

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPress

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

    Thanks in advance.

  33. Hey Micah,

    some issue I’m having with redirecting domains on clients server
    with multiple domains on one CMS / Server.

    What I would like to have:

    RewriteCond %{HTTP_HOST} ^www.domain1.com [OR]
    RewriteCond %{HTTP_HOST} ^domain1.com
    RewriteRule (.*) http://domain1.com/pages/domain1-nl/nl/home.php [R=301,L]

    (the part: ‘domain1-nl’ is the same as the domain and is a page/section for certain languages in website)

    Doesn’t work, it keeps ‘looping’…

    This (below) does work:

    RewriteCond %{HTTP_HOST} ^www.domain1.com [OR]
    RewriteCond %{HTTP_HOST} ^domain1.com
    RewriteRule (.*) http://domain2.com/pages/domain1-nl/nl/home.php [R=301,L]

    But doesn’t rewrite the TLD properly…

    Even the provider didn’t had a clear answer… ;)

    Kind Regards,

    Patrick

    1. Patrick,

      I’m not entirely sure what it is that you are trying to do. The first code block will redirect any request to domain1.com to the url in your rewrite rule… even the URL in your rewrite rule: hence the infinite loop. The second block works because it is taking all requests from domain1.com and forwarding them to the URL you provided for domain2.com. I’m not sure what you mean when you say that it doesn’t rewrite the top level domain properly. It does exactly what your rule specifies. I assume you are trying to accomplish some sort of redirect within the domain1.com site, but the initial code block doesn’t illustrate what you are trying to do… which is why I am confused. If you can clarify exactly what you do ( and don’t ) want redirected, I should be able to point you in the right direction.

      1. The server got multiple domains (by DNS) and each of the domains e.g. domain1.com, domain2.com etc. I want to redirect with htaccess to the a certain page/section in the CMS (for SEO and practical purposes).

        I’ll send you the website-link by email.

        Thanks in advance,

        Patrick

    2. Thanks for the info… the solution;

      Options +FollowSymLinks
      RewriteEngine On

      RewriteCond %{HTTP_HOST} ^(www.)?domain1.nl$ [OR]
      RewriteCond %{HTTP_HOST} ^(www.)?domain1$
      RewriteRule ^(/)?$ http://domain1.com/pages/domain1-nl/nl/home.php [R=301,L]

      RewriteCond %{HTTP_HOST} ^(www.)?domain2.nl$
      RewriteRule ^(/)?$ http://domain2.nl/pages/domain2-nl/nl/home.php [R=301,L]

      RewriteCond %{HTTP_HOST} ^(www.)?domain3.nl$
      RewriteRule ^(/)?$ http://domain3.nl/pages/domain3-nl/nl/home.php [R=301,L]

      etc. etc.

      Also added some extra php code in the config of the CMS so the
      TLD is kept when navigating to other pages…

      Kind Regards,

      Patrick

  34. Micah,

    What is the best way to redirect .com site to .mobi – through .htaccess or a script in HTML code? Currently, I use the following script on my desktop site (www.mysite.com):

    DM_redirect(“http://www.mysite.mobi”);

    The problem I have with this script is when I open the site on a mobile device and click on “SEE FULL DESKTOP SITE,” I get redirected back to the mobile site (a loop). It never takes me to the .com website. I appreciate your help and input. Thank you.

    Best regards,

    Derek

    1. Derek,

      Personally, I would use .htaccess to handle this redirect, but you could do it through a number of different methods. It sounds like you should be setting a cookie on the users mobile device that indicates whether they prefer to see the mobile site or the full site. You can use .htaccess to conditionally redirect from the .com domain to the .mobi domain based on the value of the cookie or lack thereof.

    1. That isn’t a command you would use in the terminal. On Apache servers, you would just place the .htaccess file at the web root of your old domain and place the redirect code into that file.

  35. Please help. How can I redirect a jpg URL (from /images) to an htm URL page (to /)?

    from:
    mydomain.com/images/ring.jpg

    to:
    mydomain.com/ring.htm

    Your help is appreciated.

    Thank you.

    1. I am not entirely sure what you are wanting to accomplish. It is entirely possible to redirect from an image url to a page url, but the circumstances around why you might want to do that elude me. Also, is this a one-off redirect or are we trying to do this on a larger scale? If we are working with multiple image urls, are all of these images going to be jpegs?

      1. Hi Micah,

        Thank you for your reply.

        I added 6 new jewelry images to a secret pin board in Pinterest before I created the actual product html pages on our website. I invited select people to view the secret pin board in advance of publishing the html pages on our site. This treatment was meant to make the invitees feel special. It worked because many accepted my invite and became followers. They also repinned our jewelry and many of their followers are also repinning.

        The repinned images all point back to my domain.com/images and I wanted to redirect to domain.com/*.htm so I can get the traffic counts thru google analytics.

        We don’t typically come out with two new lines at once. And this was my first time to make a secret pin board and experiment.

        That’s the explanation. Can you help me please?

        1. Bob,

          Here is how you would redirect using the mod_rewrite module:


          RewriteEngine On
          RewriteBase /
          RewriteRule ^images/([^.]*).jpg$ http://mydomain.com/$1.htm [R=301,L]

          I haven’t tested that code, so it could have a bug in it… but it should do exactly what you want. It does assume that your images end with the .jpg extension.

          You could also setup a one-off redirect for each of the six images using this type of syntax:

          Redirect 301 /images/ring.jpg http://mydomain.com/ring.htm

          This might be a better option as the general redirect above will impact all of the images in your images directory.

  36. Hi,

    How to clone my website.
    I want to use the same data when I open mydomain1 and mydomain2. I do not want to be redirected to another site or use a bridge. Please, help. Thank you!

    1. @artiss,

      Unless you want to have two separate sites that could potentially get out of sync, then you will want to have one data source and point both domains at the same source. This could be done through .htaccess, but doesn’t fall under the topic of 301 redirects. I would question why you want to have two domains pointing to the same site. This will cause duplicate content issues and would not be a good strategy if you care about the search engines. One of the reasons I wrote this blog post is to help people use 301 redirects to prevent duplicate content issues.

    1. @alex,

      I would just use the simple modAlias syntax:

      Redirect 301 /folder http://newdomain.com

      Just place that in the .htaccess file on your old domain and it should redirect anything in the folder/ directory to the new domain.

  37. Hello,

    I have a couple virtual hosted websites. One of these (www.Site1.com) contains WordPress.
    I would like a different site (www.Site2.com) to point to the WP instance on Site1.

    I did this with:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^Site2\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.Site2\.com$
    RewriteRule ^/?$ “http\:\/\/Site1\.com\/wp\/xyz” [R=301,L]

    However, is there a way to do this redirect while leaving the http://www.Site2.com URL displayed in the browser’s address bar?

    Thank you.

  38. Hi. i had a domain ( signinworld.com) this domain expired and i did not charge again. newly i registered new domain. ( megainfo.ir)
    it was redirected to old domain, i called to support web host company and she told me delete redirected domain from .htaccess file. unfortunately i do not have any information about this and i do not know any thing about programing and scripts. this is my web site. http://www.megainfo.ir please visit from my web site and tell me what problem is in my web site and how can i solve my problem? Thnaks…

    1. @elena,

      Try this:

      RewriteEngine On
      RewriteBase /
      RewriteRule ^site1(.*)$ http://www.mydomain.com/site2/$1 [R=301,L]

      Also, please note that the location of your .htaccess file can have a lot to do with whether this works or not. This should work if it is at the site root (and there is not .htaccess file in your site1 folder), or if the .htaccess file is in your site1 folder. This will NOT work if it is in your site2 folder.

      Additionally, you have to be careful when testing 301 redirects. Here is a helpful article on dealing with the caching of 301 redirects.

  39. Dear Micah,

    thank you for your immediate reply!
    I have .htaccess files in both folders (site1 and site2)!
    Also, the .htaccess file of the root folder is locked..
    Do you have any other suggestion for my problem?

    Thank you!
    Elena

    1. Basically, Apache will check the current directory for an .htaccess file and move up one directory at a time looking for one if it doesn’t find one in the current directory. This allows you to create a .htaccess file in a specific directory that overrides a more generic one. You should be able to just add the rules to the .htaccess file in your site1/ directory and have it work. Of course, you probably want to add it to the top of the file if there are other contents in there.

  40. Dear Micah,

    finally i added a javascript redirection in my index.php of site2.
    It seems to work fine!
    If you have any other suggestion please i would like to send it.

    Thank you!!
    Elena

    1. You added a javascript redirection to the index.php of site2? I thought you were trying to redirect from site1 to site2? If you are doing the opposite, you may have to reverse the directions I gave you earlier.

  41. Hello Micah,

    I am switching over from old site to new site. But I want the redirection as follows.

    –> Redirect the home page of the old site to one of the sub pages of new site

    –> Redirect the other pages of old site to the respective pages of new site.

    –> Redirect from non www to www for the new site.

    What I need to do to include all three rules in one .htaccess file..?

    Thanks in advance
    Julian

    1. Julian,

      You can’t actually do all three in one .htaccess file. The first two items will need to be handled in the .htaccess file on the old site. The non-www to www redirect will be handled by the .htaccess file on the new site.

      For the non-www to www redirect on the new site, just add this to your .htaccess file ( replacing example.com with your own domain ):

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

      On the old site, it sounds like you will have a bunch of one-off redirects. For simple one-off redirects, I prefer to use mod_alias redirects in the .htaccess file, like this:

      Redirect 301 /page-on-old-site http://www.example.com/page-on-new-site
      Redirect 301 / http://www.example.com/my-landing-page

      The trick here it to be sure your most specific redirect rules take place first. The second rule above will forward all traffic to the site that wasn’t caught by a previous rule to the landing page on the new site.

  42. Hi,

    I have a new domain with no content. There are pages in the old domain that i want the new domain to have.. basically instead of people seeing http://www.olddomain.com/page1/ they will see http://www.newdomain.com (no content, no hosting), when i tried domain forwarding I noticed that the URL is not changing at all even if i click outside domain links – i keep seeing http://www.newdomain.com. Not sure if i really need to transfer content, need aliasing… Thanks in advance.

  43. Hi Micah,

    I wanted to redirect one page in the old website to a new domain name. The new domain name doesn’t have any content. Is this possible with 301?

  44. Hi Micah

    First of all I have got to say that I am very impressed with your helpful site.

    I am basically looking for clarification as I am a newbie. I am doing the 301 redirects for a client who’s redirecting his old domain to a new domain and I presume I need to use this code on the old domain htaccess file:

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

    So good, so far. The new site has different url’s for all the pages and I am unsure where to put the redirect code. Do I put the following code underneath code shown above in the old domain htaccess file or imput this into the new domain htaccess file?

    redirect 301 /oldfile.htm http://www.new domain.com/newfile.htm

    Many Thanks for your help

    Robert

    1. Robert,

      Ideally, all of your redirects should go in the .htaccess file on the old domain. The reason for that is to avoid stringing multiple redirects together if possible. Always start with the most specific redirects first. If you don’t, then the more specific rules will never get hit.

      So, to be more specific to your use case, the .htaccess file on your old domain would look something like this given your hypothetical rules:

      Redirect 301 /oldfile.htm http://www.newdomain.com/newfile.htm

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

  45. I have to say this works like a charm, my question is can I have a Popup or a message display before it jumps from olddomain.com to the newdomain.com, so the users can update the bookmarks etc like a notification page for a few seconds

    thanks

    1. Dan,

      It isn’t possible to have a message display when using .htaccess to manage redirects. If you want to do this, you should probably create a web page that displays a message and uses JavaScript to redirect the user after a few seconds.

  46. Hi,

    So if i use this code

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

    Do i still have to write code for the individual pages on the site or will it automatically redirect to the pages on the new site ?

    1. That code, when placed in the .htaccess file on the old site, will ensure that all URLs are properly forwarded to the new site. All of the individual URLs will stay in tact, but just the domain will be updated. So, no additional redirects should be necessary. However, if you duplicated the site on the new URL and then moved pages around, it may be that you need to create a few one-off redirects to take into account the pages you moved.

  47. Dear Micah,

    I seem to be having a problem to getting all of my redirects to work. About 90% of the redirects work, but a small number keep redirecting to non-existing links. For example:

    Redirect 301 /groepsactiviteiten/overige/beachchallenge/ http://www.natural-high.nl/events/groepsactiviteiten/beachchallenge/ keeps going back to http://www.natural-high.nl/events/groepsactiviteiten/activiteiten-met-partners/beachchallenge/ although there is no such link anymore (not in the redirects anyway).

    Any idea what could cause this?

    1. Stefan,

      The ordering of redirects could be impacting your results, so make sure you start with the most specific rules first. For example, if you have a rule like this prior to the one you pasted in your comment, it could be the cause:

      Redirect 301 /events/groepsactiviteiten/overige/ http://www.natural-high.nl/events/groepsactiviteiten/activiteiten-met-partners/

      Keep in mind that the above rule would apply to the exact URL, but also any URLs that extend off of it. For example, if you wanted to redirect subpages from within the ‘overige’ directory, you would want to ensure those redirects come before this rule.

      There are some other possibilities, but it sounds like this is most likely the issue.

  48. What is the best way to protect WordPress sites from malicious attacks through .htaccess? My blog loads too slow and I believe it was compromised. Is there a standard code to put in to block spammers/bots? However, I still want the bots to crawl the correct pages. Thank you.

  49. I have seen several examples for redirecting but i have not found one to correct the problem that i am having. I am redirecting a series of pages from an old site that are linked to the new site. Here is what i have:

    there are several pages like this that i want redirected to home page.
    store.asp?pid=35450&catid=20048

    The code that i am using currently:
    RewriteRule ^store.asp(.*)$ http://www.site.com/$1 [R=301]

    The result that i get works kinda. I get this in the URL:
    http://www.site.com/?pid=35449&catid=20048

    It redirects but keeps the information and puts it in the URL.

    Any Ideas?
    Thanks in advance

  50. Hi Micah,

    Please help me with the following. I just migrated my server and change a sub directory to a subdomain i need some help to ensure all the old links still work:

    Originally i have:
    http://www.example.com/forum

    now the new installation:
    forum.example.com

    I have many published old url such as below:
    http://www.example.com/forum/index.php?topic=438.0

    I want to make sure they still work by directing them to:
    http://forum.example.com/index.php?topic=438.0

    Can you help? Thanks.

      1. Hey Micah i went through all the helpful suggestions by you above. Those were quite effective tips. But i want to know that if i have changed 2 inner page url’s of my website then can i write the 301 redirect code for both of my url’s into .htaccess file ? For e.g can i write following code in a single .htaccess file for redirecting 2 url’s?

        Redirect 301 /product1.php#contact http://www.cotinine-urine-nicotine-test.com/contact-us.php

        Redirect 301 /product2.php#contact http://www.cotinine-urine-nicotine-test.com/contact-us.php

  51. Hi.
    I will change my actual domain name for seo purpose.
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.olddomain.com$
    RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]

    Until here everything Ok. But Also need change the pages extructures. Ex:
    http://www.olddomain.com/olddirectory/directory2/directory3/file.html -> http://www.newdomain.com/newdirectory/directory2/directory3/file.html

    Can you help me with that pls.

    1. I would just add this line before your ‘RewriteEngine On’ line:

      Redirect 301 /olddirectory http://www.newdomain.com/newdirectory

      This will ensure that anything from either domain that has the old directory is redirected to the new one. The mod_rewrite block will handle anything that this simple mod_alias rule doesn’t catch.

  52. Hello Micah,

    At the moment, our site is http://unlimitedgamestore.com/store/ and we want to redirect all old domain traffic to http://www.new.com. The question is, we won’t change anything in the structure, in fact, we will use the same server (we use AWS). So, will this change anything? Or following your steps should be enough?

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

    I’m looking forward to your reply :)

    Thanks in advanced.

    Regards,
    Borja.

    1. Borja,

      Using the rewrite rules as you listed them in your comment would ensure that all URLs are redirected to the new domain without changing the structure at all.

  53. Hello,

    Yes but, as I told you we’ll use the same server to host the site. Will this result in any issue? Also our home page is /store/ we have to include it in the .htaccess or not?

    Thanks for the help.

    Regards,
    Borja.

    1. Assuming that you are not changing your URL structure at all, then whether your site is on the same server or not is irrelevant. If you are changing your homepage URL, then you could create a one-off redirect rule for that.

      1. The structure is the same so. If the homepage is http://www.unlimitedgamestore.com/store/ we would put this on .htaccess?

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

        Or it doens’t matter? Also, when I say that we use the same server I also mean that we use the same .htaccess.

        Waiting your reply, thank you very much :)

        Regards.

        1. So, the rules you pasted above wouldn’t work, since ‘/store/’ isn’t part of the HTTP_HOST. The original rules you pasted would work just fine. All I was saying previously is that if you were changing your homepage from http://www.unlimitedgamestore.com to http://www.unlimitedgamestore.com/store/, then you would need a new redirect rule. Using the same .htaccess file is just fine. Any requests that come in from the unlimitedgamestore.com domain would automatically be redirected to new.com and anything coming into new.com would remain unchanged.

          1. Hi,

            Ok. Thank you.

            I had that doubt because, in our .htaccess our RewriteBase is /store/. So I thought that we would need to add “/store/” in the rules aswell.

            Ok so, I think my doubts are now cleared :)

            We’ll try with:

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

            And hope everything goes as it should :)

            Thanks for the help! :)

  54. Hello Micah,

    Great info here. Thanks so much for your detailed help!

    I have been a bit confused with this issue for a while now.

    I noticed that I have an old .ca which is indexed in google that is redirecting to my .com address ( i think at the registrar??) I am going to remove this set up at the registrar-but want to be sure that any links or people typing in the .ca address continue to be taken to the original .com address only.

    Does this code .htaccess code appear correct:

    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^www.otherdomainname.ca$ [NC]
    RewriteCond %{HTTP_HOST} !^www.maindomainname.com$ [NC]
    RewriteRule ^(.*)$ http://www.maindomainname.com/$1 [L,R=301]

    *I then have a lot of redirect 301 here which redirect from old urls to the new proper pages.

    1. @dane: You will not be able to do a 301 redirect from otherdomainname.ca to maindomainname.com using an .htaccess file on maindomain.com. You will have to do that in an .htaccess file on otherdomainname.ca.

  55. Is it possible to use a wildcard in a redirect *.jpg so that all images that previously were at the root (/) are redirected to a subdirectory /old_site or something similar?

    I made the mistake of having an older website with over 5,000 images at the root.

    I’ve moved my website to a new host and put a copy of the old website in a subdirectory. I’ve created 301 redirects for the html pages, but am now realizing that there are hundreds, if not thousands, of images that google has indexed. They come up as file not found from the google images page.

    Thanks in advance.

    Allan

    1. Absolutely. I’m thinking that something like this is what you need:

      RewriteRule ^(.*)\.jpg$ /images/$1.jpg [R=301,L]

      You would just need to replace ‘images’ with the subdirectory you are using.

  56. Thanks for this great article! :-)

    It gave me exactly what I needed to set up a proper 301 redirection from all pages on an old domain to the equivalent pages on the new domain. You saved me a lot of trouble!

  57. Hello Micah, it’s very nice that you help people out of good will. I admire that. If I may join the discussion with a problem of mine, I would be very grateful if you could help me out. The case is the following: we have a website hosted onsite on a centos machine with apache running on it. We’ve been using the same domain name for many years and now we bought a different one. I was asked to redirect the old domain name to point to the new one so people will see we have a new domain name. I tried the suggestion on your post but i think i created a loop because i’m redirecting to the same machine. Have you encountered this issue and do you know what i can do about it? Thank you in advance

    1. It shouldn’t matter if you are doing the redirect on the same machine as long as you are using the conditionals to ensure you are only forwarding requests from the old domain. So, this block of code should work if placed at the top of your .htaccess file (updating the URL references, of course):

      <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
        RewriteCond %{HTTP_HOST} ^www.olddomain.com$
        RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
      </IfModule>

      The RewriteCond lines ensure that only requests for olddomain.com are forwarded.

      1. I feel pretty stupid. It seems my firewall was rewriting the host header and I would always receive the host header of the old domain even if accessing the site by the new domain name, hence the loop. The original block of mod_rewrite code was working properly. It’s the firewall that was messing things up. Thank you so much for your help.

  58. Hey, Please help.
    I need to redirect my old site to new site. New site is having different site structure from the old one.
    Can I still redirect few pages as well as the domain name from old to new site pages ?

    1. That is absolutely possible. Here is what you do:

      Just change the domain names in the block below and make sure it is at the BOTTOM of your .htaccess file on the OLD site.

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

      Then, for any specific pages that aren’t going to map to the same path on the new URL, you will need to add a one-off redirect like this:

      Redirect 301 /some/page.html http://www.newdomain.com/some/relocated/page.html

      Note that these one-off rules will need to be BEFORE the other block of code. The rule of thumb is that you start with the most specific redirects, then anything that slips past those gets caught with the generic redirect.

  59. Hi there Micah!
    is it possible to have a wordpress instalation on a sub-domain of mine ( blog.elevar.eu )
    and access it through another domain i bought?
    I got http://www.ohpinas.eu precisely for the wordpress site i have installed on blog.elevar.eu and i want the site to be fully accessible through http://www.ohpinas.eu without me moving the instalation or what not.
    So far i could redirect the website to http://www.ohpinas.eu but i cant access all the content/posts/pages through that domain!
    for example:

    http://blog.elevar.eu/verao-nas-redes-sociais/

    the above link works but i want it to work as:

    http://www.ohpinas.eu/verao-nas-redes-sociais/

    is it possible? if so, how? :D

    Thank you in advance for your time and i’m sorry for my english!

    1. It is possible. I believe the ‘Dynamic Mirror’ section on this page is what you are looking for: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html. Also, keep in mind that once a visitor is on a page and the URL is successfully manipulated, that doesn’t mean that the links in WordPress will point to your desired URL. You might be able to change the home URL in the WordPress settings to point to the new URL, but that is something I would recommend testing…

Comments are closed.