How to Change the URL of a WordPress Site

Sometimes the URL or domain for a WordPress site is no longer appropriate. This could be for several reasons. Perhaps everything was setup using a different domain (like localhost) for testing purposes. Maybe, it was installed in a folder and now needs to shine at the root of the site. Maybe the domain has been penalized as spammy by major search engines but the content still has value. Or maybe the URL wasn’t very well thought out and rebranding needs to occur. Or maybe it simply needs to be moved over to a “/blog/ folder. Whatever the reason for the URL change, there are a few steps involved in making it happen.

One thing to keep in mind: A lot of times URLs are hardcoded in the database for media uploads and internal linking, or plugins or themes may do that as well. All those instances will have to be changed as well, and often require direct database editing.

For Domain Changes an Extra Step is Required

If changing the domain, it’s mandatory that the server is aware of the domain as well. If it’s the main domain for the hosting account, this might require a ticket to be put in with the hosting provider in order to be accomplished. Or it may require just a simple move of the content (or backup of files and database), and then typing the new domain in a field. It will be different for different hosts. Check with their help files or support steps for assistance.

If it’s an add-on (or secondary) domain, then it’ll need to be setup and pointed to the existing installation folder on the server. Then the domain itself will need to have the name servers changed to point to the server.

To add an add-on domain, if using CPanel, perform the following steps:

  1. Login to your hosts’s CPanel.
  2. Scroll down to the Domains section and click on Addon Domains.
  3. Type in the domain name in the New Domain Name: box.
  4. Specify the Document Root in relation to the “home” folder.
  5. Create a password (which can be used for the main FTP user for that particular site – it may never be used though, as the FTP account for the main hosting account is often used to gain access).
  6. Click the Add Domain button.

Make sure the site is either moved to, or still resides at, the location selected on the server. Also make sure the Name Servers are set for that domain at the domain registrar. To get the two name server values to use, go back to the home screen of the CPanel, and scroll down to the bottom left side and locate the Account Information box. The server values are in there.

Option 1: First Try Changing the Necessary Values for the URL Switch from the WordPress Dashboard if you Are Able to Gain Access

Many times a simple change can take care of the URL switch. If you have access to your WordPress dashboard, you may be able to make the change to the URL right in there. If so, head to Settings >> General. Then modify the “WordPress Address (URL)” and “Site Address (URL)” values appropriately. Unless installing WordPress into a subfolder, these values are generally set the same.

Option 2: Hard Coding the Change in wp-config.php

If you cannot access the WordPress dashboard, these values can be hardcoded into the “wp-config.php” file. This file will exist in the root of the WordPress installation. It can be downloaded by FTP (or the Control Panel’s File Manager). Depending on the tool used, it might be able to be modified directly. Create a backup somewhere before modifying. This approach isn’t ideal as it makes it so the values found on the General Settings have no meaning. The values, if existing in the “wp-config.php” file will take precedence. At any rate, if it is necessary, use the following commands to change the values.

define('WP_HOME','http://new-location-example.com');
define('WP_SITEURL','http://new-location-example.com');

Option 3: Setting the Change with Temporary Commands in the Theme Functions File

A better approach to the above (editing wp-config.php) would be to make a temporary modification to the functions.php template file to change those values. This is meant to update the values in the database, which in turn reflects the correct values shown in the “General Settings” area.

You can access the existing functions.php file from FTP or some other method. It should be located in the “/wp-content/themes/CURRENT-THEME/” folder. Naturally, “CURRENT-THEME” in that path is the folder for the theme currently being used. If functions.php doesn’t exist, you can create it with a text editor. If so, do a File >> Save As… and put the full file name in with the .php extension.

Either way, this is a temporary change to this file and should be removed after the change is successful.

Within an existing functions.php file, add the following lines just after the first instance of “<?php” seen in the file (near or at the top).

update_option('home','http://new-location-example.com');
update_option('siteurl','http://new-location-example.com');

When creating a NEW functions.php file, add the following:

<?php
update_option('home','http://new-location-example.com');
update_option('siteurl','http://new-location-example.com');
?>

Save the file and upload it, overwriting the existing file (if exists). Make sure you have a backup. Now, after refreshing the “/wp-login.php” page a time or two at its new location, you should be able to gain access.

Last Resort: Changing the URL Values in the Database Directly

Failing any of the above, you can change the values directly in the database itself. Now, these steps aren’t to show you how to change the new URL path from within posts, and other areas where plugins or themes (or WordPress) hardcoded the path.

If you find that some of your media or internal links in posts aren’t displaying properly, follow through the steps found in the Changing The Site URL doc in the WordPress Codex. Scroll down near the bottom of the file where you will see some SQL commands to run. Pay particular attention to the warning about the GUID values. Those are unique values that use the domain/site URL at the time of posting and should not be changed as they serve a different purpose. Even when changing the URL, in most cases (except the one noted at the above link), those values should remain in tact. If changed it will update your RSS subscribers as if all the content on your site is suddenly new again, among other factors.

Finally, the steps for modifying the database directly to change the the “WordPress Address (URL)” and “Site Address (URL)” values:

  1. Backup the WordPress database (either with a plugin or XML/SQL export).
  2. Login to CPanel and login directly to phpMyAdmin.
  3. Depending on your server you may need to select the database that you want to work with from the left side. All the tables within your database should appear on the left. They will have whatever prefix was set during installation. By default that would be “wp_” but yours may be different. I’ll assume “wp_” for the rest of the steps though.
  4. Look for “wp_options” from the list, and click it.
  5. By default you should be on the “Browse” tab, but if not, click the Browse icon/link near the top. You will then see a screen on the right with all of the fields within that table.
  6. Below the “option_name” field, look for “siteurl.” It is likely near the top unless the row has been sorted.
  7. Click the “Edit” icon/link which should be displayed near the far left of the row. You will be able to modify the “option_value” field now from the new screen that appears.
  8. Carefully make the change, verify it, and double verify it. Then click the “Go” button just below.
  9. Now look for “home” underneath “option_name.” It likely won’t be on the first page. Just above the list of fields you should see “Page number:” with a drop-down and some arrow buttons. Scroll through by clicking the single arrow button until you locate the “home” value (probably page 2).
  10. Click the “Edit” text link and modify it in the same way that you did “siteurl.”

You should now be able to visit the WordPress admin dashboard or login page at its new location. Remember, there still may be the need to manually (or with an SQL statement) modify values in the database for posts that have hardcoded media paths and internal links within them. Read through the Changing The Site URL doc to learn how and don’t skip the warnings about the GUID. It may present a problem for your existing subscribership if ignored.

Found this article interesting? Subscribe to Tips and Tricks HQ

email icon rss feed icon twitter icon google plus icon

Comments (3 responses)

  1. madhu says:

    Excellent stuff, really very helpful. I was soo much disappointed when I lost access to my site. Worked hard referred to many blogs but din’t help. Your videos were really helpful in sorting this out. Thank you very much!!

  2. Wallace R says:

    Great tips. Thanks for sharing with the community.

  3. Ritugupta says:

    If your site is important enough that you don’t want to lose it, you always want to make sure you have a backup first.

    Then you’ll want to proceed with the query given, which should update any incorrect URLs within your posts.

    If you accidentally mess something up, you can revert back to your backup. Your host probably has a backup, but don’t rely on that… make your own just in case.

Speak Your Mind

*