After Moving a Site to Another Domain, All Images Are Lost

I moved from www.domain1.com to www.domain2.com

However, the image paths are still pointing at www.domain1.com/wp-content/uploads.... So when www.domain1.com went down, all my images are not showing anymore.

I updated the wp_posts table where all attachments now point to www.domain2.com/wp-content/uploads..., but the images exist in www.domain2.com and I still cannot see any image. Is my caching is off?

Topic domain uploads images migration cache Wordpress

Category Web


You can use the Wordpress plugin: Go Live Update Urls

It is really easy and quick.


Use WP Migrate DB for export data based zip with replace your url.

using this plugin you can not lost widget or any serialize data.

enter image description here


very easy method

Install this plugin Velvet into your wordpress backend.

Now go to the velvet setting and enter your old domain and enter new domain. hit update button. You are done


Although you say that you've updated your wp_posts data to www.domain2.com, there is still the home and siteurl in your wp_options table that needs to be changed amoung other things.

To ensure all of your data has been updated to your new domain, please go through the following steps:

  1. Go and download Interconnect IT's Database Search & Replace Script here
  2. Unzip the file and drop the folder (via FTP) where your WordPress is installed (the root) and rename the folder to replace (screenshot)
  3. Navigate to the new folder you created in your browser (ex: http://www.domain2.com/replace) and you will see the search/replace tool
  4. It should be pretty self-explanatory up to this point: enter your old URL in the search for… field and the new URL in the replace with… field

You can click the dry run button under actions to see what it will be replacing before you execute the script. Once you're done be sure to remove the /replace/ folder.


Once inside the cPanel, find the Databases category and click on the phpMyAdmin tool icon. You are now on the main phpMyAdmin page. Find the newly added database and click on its name from the left-hand sidebar. Our sample database is named testdb. This opens the database and the screen refreshes with a list of the tables. Using the sidebar again, find and click on the wp-posts table. Look to the top of the screen and you will see several tabs that run across the page. Click on the SQL tab. You are now on a MySQL editor screen. In the code area, copy the following bit of MySQL.

UPDATE wp_posts SET post_content=(REPLACE (post_content, 'dommain1.com','domain2.com'));

Might work ?


Sounds like you might already have your answer but I strongly recommand you to use DBSR (db search replace) to replace ALL occurrences of domain1 by domain2. There are tools for that. This one is pretty clean : https://github.com/interconnectit/Search-Replace-DB

The whole process is described. There is a good UI.

This way you'll be sure everything is ok regarding DB records.


If your site is fairly small, you could...

  1. Log into phpMyAdmin.
  2. Export your database as an SQL file.
  3. Make a copy of your database SQL file so you have a backup of your original database, in-case you screw up.
  4. Open up your database SQL file in a text editor.
  5. Find and replace domain1.com with domain2.com.
  6. Save your database SQL file.
  7. Log back into phpMyAdmin.
  8. Import your database SQL file.

EDIT 03/18/2016

AFTER you've done the above steps, please proceed with the following procedure:

Step 1 – Create a function to use.

function mbe_migrate_widgets() {

}

Step 2 – Specify what to search for and what to replace with.

function mbe_migrate_widgets() {

    $old_domain = 'http://dev.';
    $new_domain = 'http://beta.';

}

Step 3 – Retrieve all WordPress widgets.

function mbe_migrate_widgets() {

    $old_domain = 'http://dev.';
    $new_domain = 'http://beta.';

    global $wpdb;

    // Get all Widgets
    $widgets = $wpdb->get_results(
        "
        SELECT `option_id`, `option_name`
        FROM $wpdb->options
        WHERE `option_name`
        LIKE '%widget_%'
        "
    );

}

Step 4 – Loop through all WordPress widgets, and retrieve the information for each widget.

function mbe_migrate_widgets() {

    $old_domain = 'http://dev.';
    $new_domain = 'http://beta.';

    global $wpdb;

    // Get all Widgets
    $widgets = $wpdb->get_results(
        "
        SELECT `option_id`, `option_name`
        FROM $wpdb->options
        WHERE `option_name`
        LIKE '%widget_%'
        "
    );

    if ( ! $widgets ) {
        return false;
    }

    $actions = array();

    foreach ( $widgets as $widget ) {

        // Retrieve the Widget data.
        $widget_data = get_option( $widget->option_name );

    }

    return $actions;

}

Step 5 – Perform the search and replace on the widget data and update the database records.

function mbe_migrate_widgets() {

    $old_domain = 'http://dev.';
    $new_domain = 'http://beta.';

    global $wpdb;

    // Get all Widgets
    $widgets = $wpdb->get_results(
        "
        SELECT `option_id`, `option_name`
        FROM $wpdb->options
        WHERE `option_name`
        LIKE '%widget_%'
        "
    );

    if ( ! $widgets ) {
        return false;
    }

    $actions = array();

    foreach ( $widgets as $widget ) {

        // Retrieve the Widget data.
        $widget_data = get_option( $widget->option_name );

        // Update the Widget data changing old domain to new domain.
        $update = update_option(
            $widget->option_name,
            recursive_array_replace( $old_domain, $new_domain, $widget_data )
        );

        if ( $update ) {
            $actions['success'][] = $widget->option_id;
        } else {
            $actions['failed'][] = $widget->option_id;
        }

    }

    return $actions;

}

Step 6 – Include custom recursive array replacement function.

// Recursive String Replace - recursive_array_replace(mixed, mixed, array);
function recursive_array_replace( $find, $replace, $array ) {

    if ( ! is_array( $array ) ) {
        return str_replace( $find, $replace, $array );
    }

    $newArray = array();

    foreach ( $array as $key => $value ) {
        $newArray[ $key ] = recursive_array_replace( $find, $replace, $value );
    }

    return $newArray;

}

There you have it. Run that function once and all of your widgets should magically start working again.

Note: It's up to you where you place this code or how you execute it. Please also replace the variables $old_domain and $new_domain accordingly.

Extra Note: Please also keep in mind, this example only effects widgets. If you have other parts of your website which contain URLs in serialized data, please modify your database query accordingly. ( I'm talking about this portion of the code: SELECT option_id, option_name FROM $wpdb->options WHERE option_name LIKE '%widget_%' )

I would post a link to my blog about this topic, but don't wish to appear "spammy". This is essentially a copy/paste from an article I wrote on my website as of 03/18/2016. The contents in this answer might become outdated and the content in the article on my website may become more relevant in the future. Please, if you're interested, check my profile for a link to my website.

Useful explanation of serialized data: Anatomy of a serialize()'ed value.


hi if you are awaire database than use these steps.

  1. Log into phpMyAdmin.
  2. Export your database as an SQL file.
  3. Make a copy of your database SQL file so you have a backup of your original database, in-case you screw up.
  4. Open up your database SQL file in a text editor.
  5. Find and replace domain1.com with domain2.com.
  6. Save your database SQL file.
  7. Log back into phpMyAdmin.
  8. Import your database SQL file.

At the risk of answering the question with only a link, I think the following steps at this page (http://codex.wordpress.org/Moving_WordPress) are what you're looking for:

Changing Your Domain Name and URLs

You need to change all instances of your old domain name to your new domain name in the database safely. You can do that using one of the following:

  1. Only perform a search and replace on the wp_posts table.

  2. Use the Search and Replace for WordPress Databases Script to safely change all instances. (If you are a developer, use this option. It is a one step process as opposed to the 15-step procedure below)

  3. Optionally, you can also use an online tool (in case you don't want to bother with uploading scripts to your server) Online WordPress Serialized PHP Search and Replace which allows for replacing an old domain or URL with a new one while fixing the serialized data at the same time.

There's a lot of other helpful WP migration information on that page as well.


It sounds like you need to change the URLs for the old images. The easiest & quickest way of doing that is to do a find and replace on the MySQL database.

You can use PHPMyAdmin (usually provided by your web host) or other MySQL editing software such as Sequel Pro to make changes to your MySQL database without touching the WordPress admin area. This means you can make wide-scale changes by running SQL queries instead of making them one-by-one, which is tedious and time-consuming.

Remember to take a backup of your database first. To do this in PHPMyAdmin, select your database from the sidebar on the left, then click "Export". The "Simple" method is fine, but if you want more control over it then the "Custom" method gives you plenty of options.

After taking your backup, click on the SQL tab and paste the following into the textarea:

UPDATE wp_options SET option_value = replace(option_value, 'oldurl', 'newurl') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'oldurl','newurl');

UPDATE wp_posts SET post_content = replace(post_content, 'oldurl', 'newurl');

UPDATE wp_postmeta SET meta_value = replace(meta_value,'oldurl','newurl');

And replace "oldurl" with your old URL and "newurl" with your new URL, keeping the http:// but removing trailing slashes. Double check it, then when you're happy with it, click the Go button.

Navigate over to your site and check whether your images are working. If not, it's usually because of a typo, which you can fix by restoring the database from the backup you made at the beginning of the process and trying again.

There's a expanded guide to WordPress domain migration at this site, which also has a link to a script to generate the SQL query for you.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.