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. So, I tried what you said and here's what happens:

    I type in example.com and it goes to the old joomla site. I type in wordpress.example.com and it goes to the old joomla site at example.com

    I just went to the DNS controls in 1and1 and changed the destination of example.com to the folder where my wordpress files are. I'm hoping that is the missing link. If not, please do let me know!

    Thanks

  2. @JP: well to be honest, there is no big reason for it.

    But, it's always better to serve your files conventionally. That way future updates and maintenance will become easier.

    For example, you may employ some other person to do maintenance for you, but at first glance he might be lost, since in most cases, he'll have no idea what's happening.

    So, if it's not too much trouble, then I suggest you do it in the conventional way and move wordpress to the server root.

    Although, if you maintain all the followup updates yourself, then I don't see much difference in either solution. So do what feels easy ๐Ÿ™‚

  3. Thanks for the post. I'm having a different problem and I can't find posts or tutorials on if this is possible.

    I have my current site at example.com it's joomla. I want to replace it with a WordPress site. So, i built a test site at wordpress.example.com hoping that I could just direct example.com to the folder on my server where all the files are for wordpress.example.com is this possible? Can I just do this with DNS?

    Most of the posts I'm finding are talking about changing hosts. I'm looking for a simpler solution if it exists.... I hope this isn't too off topic.

    Thanks!

    1. I'm assuming your wordpress is in the folder wordpress in relation to web-root of example.com (i.e. the folder can be soft-linked as example.com/wordpress/).

      In that case do the following:
      1. Use the following .htaccess CODE in example.com web-root:
      # BEGIN WordPress
      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
      RewriteRule (.*) /wordpress/$1 [L]
      </IfModule>
      # END WordPress

      2. 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
      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{HTTP_HOST} !^www.example.com$
      RewriteRule ^(.*)$ "http://www.example.com/" [R=301,L]
      RewriteBase /
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]
      </IfModule>
      # END WordPress

      3. And finally, modify wp-config.php of wordpress.example.com so that it works with http://www.example.com. Add following two lines of codes in wp-config.php, preferably at the beginning of the file (just after the starting block comments):
      define('WP_HOME', 'http://www.example.com');
      define('WP_SITEURL', 'http://www.example.com');

      That should do the trick ๐Ÿ™‚
      Let me know, if you face any further problem.

      Also, checkout another post that deals with sub-domains

      1. Wow! This is very helpful! Maybe you should write a post on this so people can find it easily. I'm probably not the only who's tried to do this ๐Ÿ™‚

        Thanks!

        One follow up question. Is there any reason why I shouldn't do it this way? Would it be better for me to just move my wordpress installation to the root on my server?

        Thank you for your help!

  4. Hello, I really want to change my WordPress url but all this stuff is too tricky for me as I'm still young and have never learnt any of this before. If you could help me, please contact!

  5. Thank you for the explanation!

    I must be on a unix server (???) since all my websites are case sensitive after the / mark.

    Back to the problem which is http://www.buildgreenenergy.info/ is case sensitive after the / mark and I want to move it to WordPress. Since I cannot find a way to force WordPress to accept capital letters in the url, if I do a 301 redirect, what percentage of backlinks might I lose doing the 301?

    How long until search engines see the 301? (days,weeks or months)?

    Thanks for all the info! It has cleared up my understanding of why some url's are case sensitive and some are not.

    Jonny

    1. @Jonny

      First of all, you'll not need any 301 redirect after converting to WordPress. Even though WordPress forcefully converts all URLs to lower case, the same URLs still correspond to the upper cased version. So even in unix system, after converting to WordPress, your old Upper cased URLs will be accessible. For example, the following two URLs will be the same:

      http://www.buildgreenenergy.info/Magnetic-Motor.html
      http://www.buildgreenenergy.info/magnetic-motor.html

      Secondly, if you still do any 301 redirect, no back-link will fail to do 301. Although you'll have to keep the redirects forever. Since, search engines will correct the URLs, but some other sites (having back-link to your site) will never correct the link. They will always be using the 301 redirects.

  6. I want to convert my website to WordPress. I have the plugin needed to keep .html on the pages. However, as you can see some pages " http://www.buildgreenenergy.info/Magnetic-Motor.html " have "M" capitalized in URL. I want to save all the new pages in WordPress as they were in the old website with the capital letters. I want to keep all the URLs as they are because of all the backlinks. Do you have any suggestions how to do this? I do not want to use a redirection plugin and change the URLs.
    Thanks!

    1. Hi Jonny,

      Best way is to hire an expert and let him do the job for you. Since, there might be issues because of your server capabilities etc.

      You can hire them from freelance site like freelancer.com | elance.com etc.

      Still, if you want to do it yourself, you can follow the the instructions below:

      Assuming you have the right .html extension plugin (which should add an additional .html extension after each perma link WordPress generates):

      1. Go to wp-admin -> settings -> Permalinks

      2. Select "Custom Structure" and in the input box type the following:

      /%postname%/
      and save the setting

      3. As you create new post, after providing a post name, you'll see that permalink of that post will appear below the title. Now, set the permalink as you like (without .html of course)

      Upper cases will be converted to lower case, but don't worry, after submitting the post, you should be able to access the post with any "case" you like, since the links are case "insensitive".

      So, you'll still get all the back-links, and search engines will not demote your rating, since search engines also see the links in case insensitive manner.

      Hope, these instructions will help you.

      1. fayazmiraz,

        Interesting that you said "links are case insensitive". I believe anything after the / is case sensitive. Do you have an example of a website that is case insensitive?

        Just as there is a simple plugin for .html ( called .html on PAGES ), I am hoping to find the code or plugin for capital letters in url.

        Jonny

    2. Jonny,

      In Unix, file/folder names are "case sensitive", since most of the time a link (after the /) is also a file in the server, the link also becomes "case sensitive" in case of Unix servers.

      In windows however, file/folder names are "not" case sensitive. So any link that corresponds to a file to a windows server, is "case insensitive".

      Although, the important thing to keep in mind is that, WordPress posts do not correspond to any original file or folder in the server and WordPress engine itself considers the post URLs in a "case insensitive" manner.

      For example, my site is in a Linux Server, but it does not have any directory named "wordpress-blog-url-change", this is rather a generated link from wordpress. So although it is in a case sensitive Linux server, you'll be still be able to access this page in different cases; like:

      http://www.fayazmiraz.com/wordpress-blog-url-change/
      or
      http://www.fayazmiraz.com/WordPress-Blog-Url-Change/
      or
      http://www.fayazmiraz.com/wordpreSS-bloG-uRl-chANge/
      etc.

      However, in my server there is a file named: "favicon.ico"
      You can access this icon file from this URL:
      http://www.fayazmiraz.com/favicon.ico
      But, any other case, for example: http://www.fayazmiraz.com/Favicon.ico
      will give you a 404 (not found) error.

      Although, if my site were in a windows server, even the files would be accessible from different cased links. For example, one of my client's site is in a Windows server, and files in that server can be access from different cased links.

      I believe this is enough explanation :), Thanks.

  7. I just had this problem what I did was simply update the to the URL and save settings (which of course takes your site offline). Move it then update your .htacess which if you have moved it from a root to a root of server won't be a problem but if inside a file inside your server root then will.

    1. If you move the site inside a "folder" of your server root, in that case you'll have to update your WordPress .htaccess to make it work correctly:

      .htaccess for root:
      -----------------------
      RewriteEngine On

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

      .htaccess for "sub" folder inside root:
      ----------------------------------------------
      RewriteEngine On

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

  8. Making SQL queries is not that scary. There is a simple online tool to generate sql queries to Change a wordpress URL. There are always a bunch of settings besides the "siteURL" and "home" that you will have to update after the switch - by making these queries you can save yourself a lot of time and headache.

    1. Actually when you transfer your WordPress powered blog, from one domain to another, in that case changing database with SQL queries will not hurt, and it'll also keep your database clean.

      But for temporary URL change or for people with very few knowledge of SQL, this method will be more suitable. For example, in my case, I test my site locally first (in localhost), everytime I install a new plugin, change something in the theme etc.
      Then upload the new files to my site.

      If I used SQL to change site address, then I would have to run the queries every time I make a test. So that's why keeping the url in wp-config helps.

  9. Nice, i've moved hosting to hostgator because a lot of spammer that leave tons of spam comment that make Bandwidth Limit Exceeded just in few days.

    but how about import from localhost to public from WP dashboard, have you tried it.

    1. Import works fine, but import has two problems:
      1. If you have many plugins, custom theme and some custom changes in WordPress, then you'll have to do all those changes to your new WordPress instillation in the public domain. So evidently, in some cases you'll forget what change you actually did from the original default setting. So someway, the public version may become different than the local copy. But, transferring all the files using FTP doesn't have the same problem, if you follow my method.

      2. If you have many posts and comments, in other words a very big database, then import may create problems. Otherwise, for small database and default wordpress setting, import is fine.

    1. Russel vai, I actually found this recently, so I thought it'll help others too. Changing WordPress URL was a pain so far, since one needed to change the database entry here and there. So testing in localhost and then moving to the public domain was not as simple as drag and drop.

      Even better news is: after working on it for a while, I actually created an even better way. Hopefully, I'll be able to share it with you soon.

  10. Pingback: Dave-Miller.com
  11. Does this override the settings which is at wp_options table as field name "siteurl".?

    So far i know that you have to change the values for wp_options otherwise all things simply won't work.

    Let me know if putting those two constants do every trick.

    1. Yes, it does. So the only problem is you'll not be able to change Blog URL from "Settings" in Admin panel, since above settings will override them.

      As far as every trick is concerned, it will work only for relative URLs. So if you have any site link hard coded inside your posts, obviously that wont work. In all other cases it will work fine, at least as far as I've tested.

      Oh and it works only for WordPress 2.2 and above.

Leave a Reply

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