How to change the address of a wordpress site.
How to change the address of a wordpress site. Or as it’s sometimes referred to, how to change the URL of a WordPress site.
There are a few reasons why you might need to know how to change the address of a wordpress site:
- If you decide to operate your site on a different domain.
- If you’re moving a staging site from a subdirectory or a subdomain to the main domain.
- If you’re cloning one WordPress site to another WordPress on a different domain.
There are a few different ways to change the address of a WordPress site, and there are also some implicatons to be aware of when it comes to how to change the address of a WordPress site.
Before you begin.
What I’m going to mention about how to change the address of a WordPress site relies on some upstream factors already being in place. These are:
- That the WordPress installation is held in the document root of the domain you’re updating to (or within the respective subdirectory if you’re updating the URL to https://domain.com/directory for example).
- That the domain you’re updating to resolves to the server on which the WordPress installation is held.
Both of these will need to be in place for any of the methods described below to function.
This guide is also written around using cPanel and more specifically PHPMyAdmin to manually update the site’s database. If PHPMyAdmin isn’t available in your hosting, you’ll have to make the same changes, but using an equivalent to PHPMyAdmin.
Also, make sure you have a backup available before you try any of this.
WordPress’ URL rewriting mechanism.
For the methods I’m going to describe to make sense, it’s worth understanding how WordPress’ URL rewriting mecahnism works. Understanding roughly how this works will help you with how to change the address of a WordPress site.
For example’s sake, let’s say you type mywebsite.com in to your browser’s address bar and press return.
The page loads, but when you check the address in the browser’s address bar it’s now https://www.mywebsite.com.
So where did the https:// and the www. come from?
The very short explanation is: It comes from the site’s database.
The longer explanation is:
The request for the site reaches the web server. The web server accepts the request, and the site held in the document root of the domain being requested is called. When the site is called, the PHP that makes up the WordPress installation executes, this interacts with the database, and the address in the browser’s address bar is rewritten based on what’s in the database, and page output is generated and served to the browser.
In the context of the site’s homepage, the address that’s rewritten to comes from the siteurl and home values defined in the options table of the site’s database.
Whilst it is possible to programmatically override the URL rewriting specific to the database with some PHP defines, this can get a bit messy, as you have a database containing one URL and the site’s code overriding that with a different URL. This can potentially cause confusion when administering your site at file and database level.
For example purposes.
Purely for example purpose, I’m going to use currentsite.com as the domain of the old/current site with a document root of /current and I’ going to use newsite.com as the domain of the new site with a document root of /newsite .
Our objective here is to copy the site from currentsite.com to newsite.com then update the database.
This guide assumes that you’ve already copied the site from /current to /newsite, and also copied the database used by currentsite.com and connected it to newsite.com using the /newsite/wp-config.php.
You can find guidance covering:
- How to migrate a WordPress site.
- How to create new databases and users.
- How to trouleshoot database conneciton issues.
Method 1: Updating the URLs in the site’s database.
How to change the address of a WordPress site by updating URLs in the site’s database. This method is slightly more laborious but it’s also the tidiest (as no code is overriding what’s in the databse) and most reliable.
As I’ve mentioned above, the site files need to be in the correct directory and the domain needs to resolve correctly for this to work.
So lets say you’ve copied the site files and the database on to newsite.com and connected the site files and the new/copied database using wp-config.php file in newsite/wp-config.php, all good so far, but…
If you type newsite.com in to your browser’s address bar and press return, you’ll be redirected to https://currentsite.com
Why? Because the siteurl and home values in the site’s options table are still https://currentsite.com, so WordPress will still rewrite the request for newsite.com to https://currentsite.com due to this.
You would have thought that, based on what I’ve mentioned so far, that you’ll only have to update two URLs in the site’s database. Whilst this does make sense, unfortunately it’s unlikley to be the case, and there’s likely to be a lot of references to https://currentsite.com in the sites database, all of which need to be updated.
We’re going to use a Search and Replace plugin to mass update the URLs in the database, but to be able to use this, we first need to gain access to the WordPress running on newsite.com and it’s currently redirecting to https://currentsite.com .
To do this, you’re going to need to access the site’s databse using PHPMyAdmin, so open PHPMyAdmin:
Then expand the database you’ve copied by clicking the + next to it:
The database will then expand and list all the tables within the database, locate and click on the “options” table (the prefix may vary, but as long as you’re clicking on a table that ends with _options you’re clicking on the right table):
You’ll then see the contents of the options table, including the siteurl and home values, as you can see these are currently set to https://currentsite.com:
It’s the siteurl and home values that cause the redirection to https://currentsite.com so we need to update these to https://newsite.com to do this, double click on https://currentsite.com on the same line as siteurl:
And you’ll then be able to edit https://currentsite.com:
In the cell you’re editing, repalce https://currentsite.com with https://newsite.com:
Then click somehwere else on screen and your change will be saved.
Now do the same for the home value:
And you should end up with this:
Once you’ve made these changes, the redirecton to https://currentsite.com will stop, so you’ll then be able to log in to WordPress, and it’s this that we need to do next to be able to mass update the URLs in the site’s database, so log in to WordPress using the same credentials you used to log in to https://currentsite.com/wp-admin, but instead use these to log in here:
What we’re going to need to do next, is to update all the other URLs in the sites database to the new URL. We’re going to use the Better Search and Replace plugin to do this.
Once logged in hover your mouse over “plugins” in the menu on the left hand side, then click on “add new plugin”:
Then type this in the search box:
better search and replace
When the Better Search and Replace plugin is displayed in the search results, click on “install now” next to this plugin:
Then, when it appears click the “activate” button next to better search and replace:
After activating better search and replace, access it by hovering your mouse over “tools” in the menu on the left hand side, and then clicking on “better search and replace”:
You’ll then be presented with the “better search and replace options” in these you’ll need to:
Search for: In this box, put the old site’s address (https://currentsite.com as per the example I’ve been using)
Replace with: In this box, put the new site’s address (https://newsitesite.com as per the example I’ve been using)
Tables: Make sure you select all the tables (click on the top table, hold shift down, then click on the bottom table)
Case-Insensitive: Tick this
Replace GUIDs: Tick this
Run as dry run: Untick this
You should end up with something like this:
The click “Run search and replace”.
The plugin will then update all https://currentsite.com URLs to https://newsite.com
That’s how to change the address of a WordPress site by updating URLs in the site’s database.
Provided there aren’t an URLs defined in site files, you’re done, but if you do have URLs defined in site files, you’re goign to need to manually update these.
Finding and updating URLs or file paths in files.
You’ll have to update these manually using the file manager in your hosting.
But how do you know what to update?
If you have shell access it’s quite easy to locate files containing the ild site URL:
grep -irl currentsite.com
This will list all files that contain currentsite.com
If you don’t have shell access, the string locator plugin can be installed and used to search for text in files.
What you need to search for is the domain of the old/current site, and the directory that the old/current site was contained in.
What you’ll need ot update to is the domain of the new site, and the directory that the new site is contained in, respectively.
Method 2: Overriding what’s in the database programatically using PHP.
How to change the address of a WordPress site by overriding what’s in the database.
You can override the URL rewriting mechanism specific to the URLs defined in the database by adding some lines to the site’s wp-config.php file.
After copying the current site and the current site’s database and connecting the two using the wp-config.php file of the new site, you can add some line to the new site’s wp-config.php file.
The lines you’d need to add are (as per the example I’ve been using):
define( ‘WP_HOME’, ‘https://newsite.com’ );
define( ‘WP_SITEURL’, ‘https://newsite.com’ );
To add these, you’ll need to open the file manager:
Then locate the wp-config.php file in the document root of the new site, right click on it, then click on edit:
Then add the two lines mentioned above just above where it says “/* That’s all, stop editing! Happy publishing. */”, and save changes:
That’s how to change the address of a WordPress site, by programmtically overriding what’s in the database.
Whilst this is a quick and easy way to change the URL of a WordPress site, it can make things a bit tricky in the future.
This is because the above doesn’ t make any changes to the database, it just overrides the database based URL rewriting mecahnism.
If you find yourself in a position where you need to work out what database is used by what site, this might prove tricky as all the URLs in the newsite’s database will still look like the old site URLs.
Method 3: Updating the site URL within WordPress itself.
How to change the address of a WordPress site by using options within WordPress.
Within WordPress, under Settings > General:
There is the option to update the site’s address:
Whilst this is easy to update (it’s just a case of putting the new site’s address in the highlited boxes above, then saving your changes) there are some limitations with this.
The main limitation is that you have to be able to log in to WordPress to do this, so if you’ve already copied the site files, you still have to update the siteurl and home values in the options table of the database to be able to make this change.
You could potentially make this change on the currentsite.com WordPress, then copy the WordPress installation to the new site’s document root.
You could also maybe do something like map the new domain to the same directory as the current domain, then make this change. That would also work.
When you update the site’s address using the facility above, this should update all the URLs in the site’s database just like the way that the search and replace plugin did in the Method 1 section above. Unfortunately this isn’t always the case, so you may find that if you use this option, not everything is updated, and you’ll then have to use a search and replace plugin to make the required updates anyway (so you might as well just do that in the first place).
- How to change the address of a WordPress site can be acheived in a few different ways.
- The most laborious way (Method 1 above) is the most relaible.
- Although programatically updating the site’s address is possible, this may have future implications from and orgnisational and adminsitrative perspective.
- The URL can also be changed from within WordPress, but there are implications to doing this such as reliability and accessbility.