Change WordPress Site URL

by , , revisited on

We have by far the largest RPM repository with NGINX module packages and VMODs for Varnish. If you want to install NGINX, Varnish, and lots of useful performance/security software with smooth yum upgrades for production use, this is the repository for you.
Active subscription is required.

Not often, but sooner or later, you might need to change your WordPress address.

This may be needed when you want to secure all WordPress pages via https://, or simply change the domain.
You can change the site URL in WordPress Settings administration screen. But that is not enough.
Wordpress stores media attachment URLs and cross-links between posts, in its database. All these URLs include the base URL and will stay unchanged. So using WordPress admin to change site URL is not the best approach.

Change URL in database. WP-CLI to the rescue

We have covered the many great uses of the WP-CLI command line utility. Now let’s cover in more details how to use it for changing WordPress URL.

Change URL to include www prefix

If you’ve decided that you want to follow the standards and change the primary WordPress domain from example.com to www.example.com, run:

wp search-replace 'http://example.com' 'http://www.example.com' --skip-columns=guid

The inclusion of --skip-columns=guid is to make sure that RSS news readers will not have to download articles from your site again. However its side effect is that site images might not have their URL renamed. So if you don’t care about news readers, just run:

wp search-replace 'http://example.com' 'http://www.example.com'

Change URL in files

Now that WP-CLI did a great job renaming all those base URLs in database, some of the files might have the old URL present in the files. Let’s use SSH and navigate to the directory with WordPress files. The following command will replace all occurrences of this.example.com to that.example.com:

find . -type f -exec sed -i 's/this\.example\.com/that\.example\.com/g' {} +

This may be needed in case you have hardcoded your WordPress URLs in wp-config.php like so:


We’re done!

Changing WordPress site URL is not a trivial task due to bad WordPress design choices. But by using the right tools for the job you can get it done quickly and efficiently.

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.