Replace URL in WordPress Posts, Images and Links

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

If you migrate your website to a new domain or location, you might need to fix (replace) all the hard coded links and URLs in your posts, also you would observe that all the images links point to your old address. All these things can be fixed easily.

Method 1: Using phpmyadmin via direct modification of the database

This is easier than it appears to be, you need the access to your database via phpmyadmin. Procedure is simple and clean. Before you begin you must back up your database and save the copy off-site (The following is irreversible. There is no “undo” or “undelete” button. This is permanent. Be warned that it can do harm.). Actually you should create a duplicate of the database and then do all these operations on the duplicate database, if things don’t go well, you can always revert back to the original database (by duplicate I mean download the backup and then restore the backup to another empty database). You can make WordPress to use the duplicate database by changing the database name in wp-config.php file in your root directory. The content given below has been taken from WordPress.org site, which I have modified a bit to make it more clear. Lets first fix all the URL entries in the database.

  1. Login to phpMyAdmin.
  2. Click the link to your Databases.
  3. A list of your databases will appear. Choose the one that is your WordPress database.
  4. Look for the name of the table within your database used to hold your post information. Usually it is calledwp_posts, but it may have been changed (if you changed the table prefix during the install). Write it down.
  5. Clicking the Browse icon or link, open the wp_posts table.
  6. Look for the field that holds your post “guid”. It is usually called guid, but it may have been changed. Write it down.
  7. Click the tab at the top for SQL.
  8. Using the information you wrote down from above and the old and new URL addresses you wish to update or search and replace within your database, type in the following (CAREFULLY). Before that, make sure that you have clicked on wp_posts table, it can be seen highlighted on the menu on the left.

Now enter the following command in the SQL queries box (make sure you replace exampleoldsiteurl.com and examplenewsiteurl.com to suit your needs. This command searches for first string in guid and replace it with the second string.

UPDATE wp_posts SET guid = REPLACE (
guid,
'http://exampleoldsiteurl.com',
'http://examplenewsiteurl.com');

This is how it should look before you click on go.

It would check and replace all instances of the string1 with string2.

Now we can use this same approach to fix all the image and URLs pointing to your old domain, by running the following command (this command would replace all content with string1 with string2 in table wp_content

UPDATE wp_posts SET post_content = REPLACE (
post_content,
'http://exampleoldsiteurl.com',
'http://examplenewsiteurl.com');

It should look something like this, hit Go to replace all content.

This should fix all post and image links. Let me know if anything is not clear, or if you encounter any issues.

Method 2: Using a plugin like Search and Replace

I haven’t tried it yet, but if the direct modification of database seems intimidating to you, you can give it a try.

If you found this useful, please consider leaving a comment or liking us on Facebook or following us on Twitter or subscribing to the RSS feed to have future articles delivered to your feed reader.

Possibly related posts (generated automatically):

  1. Replace WordPress Search With Google Search
  2. Best Featured Posts / Content Slideshow Plugin for WordPress
  3. How to Redirect / Forward Entire WordPress Blog
  4. How to Convert a WordPress Blog to Blogspot (Blogger)
  5. Uninstall Grub (Replace Grub with Windows 7 Boot Loader)
  6. WordPress Blog Post by Post Redirection!
  7. Best Amazon WordPress Plugin for Adding a Product / Item
  8. Solution to FeedBurner’s “read time out” Error for WordPress

You might also like

How to Redirect / Forward Entire WordPress Blog Hackourlives...
Best Featured Posts / Content Slideshow Plugin for WordPress While there...
WordPress Blog Post by Post Redirection! You recently...
Linux Command to Fix Permissions for Files and Directories If you migrate...

About the Author

has written 1314 posts on this blog.

Meditating towards his "nerdvana", lifehacker is a teacher by the day and a nerdy blogger by the night. He lives to learn and yearns to learn living.

Write a Comment

Gravatars are small images that can show your personality. You can get your gravatar for free today!

*
Your Ad Here

Copyright © 2012 Hack Your Life!. All rights reserved.
Proudly powered by WordPress. Developed by Deluxe Themes