How to 301 redirect your WordPress blog to a new URL

March 5, 2011   ·   By   ·   20 Comments   ·   Posted in Wordpress

Having just done this to move from apartmentonesix.com to yourcodegarage.com, I figured it would be fitting to write a quick post about how to handle this sort of redirection.

What are we trying to do?

It’s important to know the goal – in this case, we want to move a site from one url to another. For example, from




However, we’re actually moving all the content and everything over – so we want to redirect the individual urls, like this:


redirects to


That way any bookmarks or incoming links pointing at the old site will redirect properly at the new one.

The Steps

First things first, you need to duplicate your WordPress installation from the old site onto the new one. Here’s the drive by version of how to do that:

On the old site:

  1. Grab a backup of your files
    You don’t have to actually grab the whole wordpress install (with it’s roughly 1000 files) to accomplish this – chances are, you’ll be just fine with just the wp-content folder. Download these files to your desktop.
  2. Make a dump of your database
    Using phpMyAdmin (or some other backup utility), get a copy of your database. Download this to your desktop also.

We’ll be back to thsi site in a bit to implement the redirect, but for now, we’ll move to the new site.

On the new site:

  1. Upload your file backups from the old site.
    If you only downloaded the contents of the wp-content folder, you’ll need to install a fresh copy of WordPress over here, and then upload your wp-contents folder, overwriting the one that was installed with WordPress.
  2. Import your database backup to the new site
    Again, you’ll need to use phpMyAdmin, or a similar client to handle this. Depending on the new hosting setup, you’re probably going to need to create a database and a database user for WordPress to use – but that should be covered by the WordPress install process from step 1. Take Note! of your database prefix. If you’re not sure what this is, you can look at the wp-config.php file on your old site (look for a line that starts with
    $table_prefix =
  3. Connect to the database
    If necessary, set up or modify the wp-config.php file for your new site, making sure it points to the database tables you just imported. You’ll know you’re on the right track when you load up the new url, and you see your blog content there, waiting for you. The styling might be messed up, and clicking any of the links will take you to the old site – but if you can see your content, you’ve done everything right so far.
  4. Switch WordPress’ location values
    If you’ve tried to log in at the new site before this point, you probably had something confusing happen – you went to wp-login.php, entered your credentials, and suddenly you were back at your old URL! This happens because of some location settings in the database files you just imported. To fix it, you need to change 2 values in your database. Again, you’ll need phpMyAdmin, or similar, to make these changes. The first option you need to edit is id 1, with the “option_name” of “siteurl”. This should currently be the url to your old site – you want it to be your new site. Change it to that, with no trailing slash:
    Site Url Option in phpMyAdminYou need to make one more change, at about id 39 – look for an option with the option name of “home”, and change it appropriately.
  5. You’re all set! The blog should now be working at it’s new location, completely.

Redirect code for your old site

All we’ve got left to do is redirect your old site to the new one. Unfortunately, this is often the scariest and most difficult part of the entire process. On the bright side, you should be able to just paste this code in and call it done.

What are we doing?

To accomplish our redirect, we’re goign to modify the .htaccess file of our old site. .htaccess is a file that you can use to give your server special instructions on how to handle certain requests to your site. Because it starts with a dot, it’s often hidden by FTP clients – so you may have to do some tinkering to have it visible and editable to you.
Here’s the code we’re going to use:

RewriteEngine on
RewriteRule ^(.*)$ http://yourcodegarage.com/blog/$1 [R=301,L]

Obviously, you’re going to want to replace “yourcodegarage.com/blog” with your own url. Just put that at the top of your .htaccess file, and you should be all set. Here’s the gist of what’s happening:

RewriteEngine on

Here, we’re just telling the .htaccess file to make sure the rewrite engine that your webserver uses is running, and able to handle the request.


We start the line with the word “RewriteRule”, because that’s what we’re defining – a rule for the server to use in rewriting the url and sending the user to where we want them.


I’m not going to get into the nitty gritty of the regular expression being used here, but what you need to know is: This section says “Match the entire url (after the domain name), and store the whole thing for me to use later in this line.”


At this point, we give the url we’d like to send the user to. In my case, I needed to send the user to yourcodegarage.com/blog, because that’s where the blog is now located. The $1 after that is telling the server to append whatever we found before to the end of this. So, if the old url was


then “my-post/comments” would be grabbed, and appended to the end of “http://yourcodegarage.com/blog”, ensuring that the user gets sent where he needs to go.
We finish up with this:


All we’re saying here is use a 301 redirect (permanent), and don’t listen to any redirect rules after this one.

And with that, we’re all set! Make sure you use Google Webmaster Tools to notify google of the change, and get to work blogging at your new url!

  1. Hi Peter,

    This is a very helpful post and thanks for that… I just got referred to codegarage by a buddy last night and plan on switching over from my current backup method to Locker ASAP.

    Anyways, I’m doing a web re-design for a client and getting their site switched over to wordpress. This post was very helpful but doesn’t really apply since the old internal linking structure from their previous CMS is kind ugly (similar to the WP default with lots of numbers and variables in the URL).

    I’ve seen a few solutions for this using plugins but setting up rules for a large site with thousands of url’s seems a bit silly if there’s a simpler solution.

    Do you happen to have any ideas or suggestions for doing 301 redirects for re-designed site with loads of pages where the internal linking structure and url’s for the new pages will be entirely different than the old url’s so as not to lose any currently indexed pages in the search engines that are sucking in a little bit of organic traffic?



  2. Hey Chris –

    I’m afraid what you’re doing is a little trickier, because there’s not necessarily an obvious correlation between the old url and the new url, which you could use in a rewrite scheme. It sounds like your best bet is probably to use a redirection plugin to manage all of your redirection, as you can get a useful interface instead of having to muck around in your .htaccess file.

    The downside is that you’ll have to enter each url redirection individually, which could really be a time suck.

    Here’s one to get you pointed in the right direction:

    Good luck!

  3. Thanks again Peter. I just talked to my programmer about this and it seems he’s devised a method of pulling all the old URL’s (about 9,700 of them) into a database and grouping them by similarity so we can narrow down the amount of htaccess editing significantly.

    I’m actually using that same plugin you recommended on a few other sites and agree it’s a good one.

    Thanks for this helpful post and the other pointers.

  4. Sounds like you’ve got a good handle on things now. 9700 URLS all being redirected sensibly (i.e. not just to the homepage) sounds messy – I hope it works out as planned.

    Good luck!

  5. Thanks Peter.

    Can you tell me if the regular expression below will work for a site that has this permalink structure:

    To a new site with just %postname%

    RewriteEngine ON
    RewriteRule ^/([0-9]+)/([0-9]+)/(.*)$ http://newsite.com/$1 [R=301,L]

    • Hey Jared –

      You’re close. The $1 at the end refers to back to the stuff inside the parenthases, and it goes in order – so in your case, $1 refers to “2010″, $2 refers to 08, and $3 refers to “how-to-develop-a-sense-of-purpose”.

      So, in your case, you’d want to replace the $1 with $3, like this:

      RewriteEngine On
      RewriteRule ^([0-9]+)/([0-9]+)/(.*)$ http://newsite.com/$3 [R=301,L]

      One other thing you might notice – I took out the leading / after the first carat in teh rewriterule. I thought that that was required, but when I tested it just now, I had to take out that slash to get things working, so give that a try too.

      • Thanks Peter. That’s what I’ll try then.

        Would this also cover any pages I have at http://www.oldsite.com/page-here ?

        Would those also then be sent to http://newsite.com/page-here ?

        Or do I need to add an additional rule below that one like:

        RewriteEngine On
        RewriteRule ^([0-9]+)/([0-9]+)/(.*)$ http://newsite.com/$3 [R=301,L]
        RewriteRule ^(.*)$ http://newsite.com/$1 [R=301,L]


      • Ok, I think maybe I have one that will work for both domain.com/year/month/postname URL’s and also domain.com/page-name URL’s:

        RewriteEngine On
        RewriteRule ^([0-9]+)/([0-9]+)/(.*)$ http://newsite.com/$3 [R=301]
        RewriteRule ^(.*)$ http://newsite.com/$1 [R=301,L]

        I just removed the “L” from the first one since it means the last rule. I think this should work since the first rule will catch any requests with numeric characters in the URL and then fall through the second one will catch and redirect everything else.

        is that correct?

  6. Jock

    Thanks for that I just successfully redirected my site.

    For those that are lazy pay someone $20 bucks to make a copy of your website on your new domain.

    Then you can go into the .htaccess files and edit it yourself or you can pay them too.

  7. Worked like a charm. Thanks for the step-by-step guide.

  8. Excellent work and thanks a lot for saving me all the effort or redirecting one by one,
    you are a star mate. thanks again for sharing.

  9. Thanks for posting this, worked like a charm.

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

  11. Does it impact if instead of restoring 1) wp-content and 2) phpmyadmin in this sequence, restore first phpmyadmin database and then wp-content.

  12. I am getting error . Can you please guide
    The requested URL /income-tax-rates-for-financial-year-2013-2014/ was not found on this server.

    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

  13. Thanks as per above steps , i am able to transfer my domain. Thanks for complete information.

  14. Hi CodeGarage,

    I’m having trouble redirecting a website of mine (the one in my name/website). It’s not using WordPress, but I figured the 301 redirect rule would still apply. It’s not working though. Do you know if what you wrote above applies to non-Wordpress websites, or is it WordPress-only?

    Thanks for your time.


  15. I have a wordpress blog that i using costum post types.
    How to redirect my domain1.com/COSTUM/post-name to domain2.com/post-name using htaccess , i mean 301 redirection.
    Thanks in advance.

Submit a Comment