WordPress blog URL change

The easiest and safest way to change WordPress blog URL.

Few days ago I've changed my WordPress blog hosting from CirtexHosting to HostMonster. I've used CirtexHosting for 2 years and HostMonster for more than a year, mainly for development purposes. So perhaps, within a few days, I'll post a review about their performance. In short, both of them provide very good web hosting services. However, this post is not about the review, it's about the temporary problem I've faced while shifting this blog hosting.

Problem Description: After you transfer your WordPress blog files from one hosting to another (or perhaps to your PC), initially you'll not be able to access your blog, until you point your previous domain to the new hosting DNS. But, before you change this DNS entry of your blog domain (in my case www.fayazmiraz.com), you may want to (and should) confirm that your new installation is working properly. For that, you'll need a temporary URL to work with your blog, for example: http://localhost/fayazmiraz/. However, according to WordPress settings, this will not work.

Solution: Since I've worked with WordPress before, I already knew a possible solution by making some changes in SQL. Still, I've Googled the problem to find the best solution, since changing SQL sometimes can be dangerous. So, if you have WordPress 2.2 or higher, then here is the best solution for you (Changing your WordPress Blog URL without using any SQL query, by hard-coding the URL in wp-config.php):

// Add the following lines to your wp-config.php:
define('WP_HOME', 'http://localhost/fayazmiraz');
define('WP_SITEURL', 'http://localhost/fayazmiraz');
// Note: replace 'http://localhost/fayazmiraz/' with your preferred temporary URL

Now, your WordPress Blog will be working with your new hard-coded Temporary or Permanent URL. In case of Temporary URL, after the testing is complete, delete those two lines and point the DNS entry of your domain to the new hosting DNS. For people familiar with CodeIgniter, this setting is very similar to what you might have done in your CI setting.

Important: Also you'll have to change your WordPress .htaccess file if you install your WordPress in a sub-directory of your web root.

This is what the .htaccess file looks like if your WordPress is installed in the website's root:

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

This is what the .htaccess file looks like if your WordPress is installed in a sub directory named /sub/ (in relation to the website's root):

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

42 thoughts on “WordPress blog URL change”

    1. Hello Rana,

      Follow these steps and you should be all set:

      1. Move (cut+paste) all files from /beta/ folder to your "web root" ("web root" is most likely one directory above "beta" folder)

      2. Change wp-configure.php to:
      define('WP_HOME','http://mylifementors.com');
      define('WP_SITEURL','http://mylifementors.com');

      3. Change .htaccess to:
      RewriteEngine On

      RewriteBase /

      RewriteCond %{REQUEST_FILENAME} !-f

      RewriteCond %{REQUEST_FILENAME} !-d

      RewriteRule . /index.php [L]

      That's it ๐Ÿ™‚

      Let me know how it turns out. I may not answer back immediately though, as I'm very busy at the moment.

      1. BTW, remember if you happen to have hard-coded links inside your theme file or post, then those links will have to be changed manually (for theme files) or using SQL query (for posts).

        Of course, in that case, you can also use a rewrite rule in the .htaccess file to redirect them to your base url, (as a quick shortcut) ๐Ÿ™‚

  1. Will this work if my WordPress self hosted site needs a redesign and I want to have a maintenance page up while I work on it?

  2. Pingback: Dave James Miller
  3. Hi Buddy, thanx a lot. I've been struggling with a sql queries to change url.
    didn't know it was that simple:)

    cheers!

  4. Ok, everything seems to be working except for one thing that I can see. I uploaded some files before the switch and so they are in wordpress.example.com/wp-content/uploads/... when I click the link that I have on wp to the files, it just takes me to the wp homepage rather than starting a download.

    Thanks,

    1. Yes, that is expected. Any hard-coded link with wordpress.example.com/.../ will not convert to http://www.example.com/.../

      You can do any one of the following (Option-3 is recommended):

      Option-1: if there is only a few links, update the links by manually editing the posts, so that links points to http://www.example.com/../ instead of wordpress.example.com

      Option-2: if manual link update is tadeous, then follow the link updating process in this POST (this uses SQL query, keep it as a last resort. Don't do it unless you have to): http://www.lancelhoff.com/wordpress-replace-url-or-text-in-all-posts/

      Option-3: Changing SQL can be dangerous. Especially, if you don't know exactly what you are doing. To be on the safe side, you an try modifying step-2 of my (previously commented suggestion):

      --------------------------------------------
      Change the .htaccess of wordpress.example.com (i.e. the wordpress folder) for blocking wordpress.example.com and allowing only http://www.example.com:
      # BEGIN WordPress

      RewriteEngine On
      RewriteCond %{HTTP_HOST} !^www.example.com$
      RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
      RewriteBase /
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]

      # END WordPress
      -----------------------------------------------

      Notice the $1 in this line:
      RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

      In fact, this is the only change you'll have to do. Let me know how it goes.

      cheers ๐Ÿ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *