Jonathan
Jonathan

Reputation: 3534

Removing URL from Wordpress Database

So I'm trying to clean up my development environment for my wordpress-based site. In the production site Wordpress database, there are thousands and thousands of references to the specific site URL.

For example, all of my permalinks have the full address. All of the images are referenced by their full address. All of the redirects are listed by their full address.

To clone my production database on a dev server, I have to:

  1. Mysqldump the whole wordpress database into an sql file.
  2. Search and replace all of the text for the production server domain name and replace with my dev server IP
  3. Import the modified sql file into my dev db.

I'm clearly not doing something right.....

Suggestions on how I can get back on the right track?

Upvotes: 0

Views: 3995

Answers (3)

WebDragon
WebDragon

Reputation: 1

I realize I'm raising this from the dead a little, but as there was no mention of this solution, I thought it important enough to make note of. We've been using this plugin for years with great success.

As opposed to hand-editing or other such pure-sql-based solutions, there exists the Duplicator plugin for Wordpress to allow for easy site migration of an existing site between live and devel and staging servers, though this basically packages up the entire site in a zip archive with an install script that walks you through the process and ensures the migration happens as cleanly as possible, along with follow up testing, logging of the process, etc.

Upvotes: 0

Obmerk Kronen
Obmerk Kronen

Reputation: 15949

Use this ( change tables prefix and domains at will ) :

  /**
    To update WordPress options with the new blog location, use the following SQL command:
    **/

    UPDATE wp_options SET option_value = replace(option_value, 'http://www.old-domain.com', 'http://www.new-domain.com') WHERE option_name = 'home' OR option_name = 'siteurl';

    /**
    After that you will need to fix URLs of the WordPress posts and pages, which translated from post slug, and stored in database wp_posts table as guid field. The URL values in this field are stored as abolute URLs instead of relative URLs, so it needs to be changed with the following SQL query:
    **/

    UPDATE wp_posts SET guid = replace(guid, 'http://www.old-domain.com','http://www.new-domain.com');

    /**
    If you have linked internally within blog posts or pages with absolute URLs, these links will point to wrong locations after you move the blog location. Use the following SQL commands to fix all internal links to own blog in all WordPress posts and pages:
    **/

    UPDATE wp_posts SET post_content = replace(post_content, 'http://www.old-domain.com', 'http://www.new-domain.com');

The last thing to do ( and only depends on your WP DB version ) is to verify in wp_options that home_urland blog_url have been replaced correctly .

Make sure the wp-config data is correct, ( including prefix ) and you are done .

I am doing this procedure at least 3 times a week in the last 3 years .. :-)

Upvotes: 1

r3mainer
r3mainer

Reputation: 24557

I've had the same problem, but got around it with a simple bash script:

#!/bin/sh
mysql -u«user» -p«password» «db-name» -e "update wp_options set option_value \
='http://dev.example.com' where option_value='http://www.example.com';"

It doesn't change every occurrence of the site's URL in the database, but it allows you to log in without being automatically redirected to the production site.

It works well enough for me anyhow. YMMV

Upvotes: 0

Related Questions