For blogger who self-hosts the WordPress blog publishing system on a web hosting server with own registered domain name, sometimes, you may decide to reorganize the blog link URL to make it tidier or to reflect new focus or theme of the blog. You may decide to change URL of your site as well. But changing URL can cause some issues. You may face broken images in all your previous posts since you just changed URL of site but didn’t update the URLs for images already added to posts.
e.g Before changes, your wordpress URL was: http://www.old-site.com & your images URL were like: http://www.old-site.com/2013/06/image.png
And After Change, your site URL is: http://www.new-site.com but your all posts are showing images from http://www.old-site.com
It’s not possible to correct URL of all images by editing each post since it can take too much time based on volume of site. A better approach is to Change URLs of all the images collectively by SQL Querying the database. Let’s see how easy it this below:
NOTE: We will be using SQL statements based on MySQL replace() function to modify the database. To run SQL queries, you need to login to MySQL database that houses WordPress tables via phpMyAdmin or login to the DB server and run MySQL client as root. If you don’t have access to it, then you can also do this from your wordpress admin dashboard by installing a plugin: Find & Replace ( http://wordpress.org/plugins/search-and-replace/ )
Go to MySQL Query mode & use the following SQL commands to fix broken links to blog in all WordPress posts and pages:
-- MySQL Query UPDATE wp_posts SET post_content = replace(post_content, 'http://www.old-site.com', 'http:/ www.new-site.com'); -- see documentation of replace function for more understanding
The above SQL Query will replace all the links for http://www.old-site.com with http://www.new-site.com in post_content (DB Table that stores all posts content) & hopefully this will fix broken images links as well since now those images URLs will be changed to new domain.
Caution: Before making changes to database & run queries, make sure to backup your database because these changes will be permanent and can’t be undone. Dont forget to export your database from phpmyadmin as .sql