Image links When Moving Drupal to Multisite - Find & Replace in Mysql

Since the release of Aegir 0.3 I've been using Drupals multisite configuration like crazy.  Mainly because Aegir uses it but also because it just makes sense.  Starting a site in multisite works great, but moving an existing site to multisite configuration can cause some problems.

For example, on a regular Drupal install the images and file uploads are stored in the "sites/default/files" directory.  On a multisite install, the files/images are stored in "sites/example.com/files" directory.  This causes a problem becuase the links to most images/files that are in content will point to the wrong location:

  • Links will look like: http://example.com/sites/default/files/myimage.jpg
  • Instead of: http://example.com/sites/example.com/files/myimage.jpg

So you'll get a site that looks like so:

If I only have 4 images on the whole site thats not really a big deal but if I have hundreds of images in hundreds of nodes I don't really want to fix each one individually.

Using Search and Replace in phpMyAdmin to Quickly Fix Links

The command for mysql that allows you to a search and replace is pretty simple:

update [table_name] set [field_name] = replace([field_name],'[string_to_find]','[string_to_replace]')

This isnt completely dumby proof as you need to know what table and field name you want to search/replace.
Most of my images are in the node body and are stored in the table "node_revisions" in the field "body".  I am going to replace anytime there is a "sites/default/files" with "sites/example.com/files".

UPDATE node_revisions set body = replace (body, 'sites/default/files', 'sites/example.com/files');

Simple enough.  I also want to change where the main file system is set at:

UPDATE files set filepath = replace (filepath, "sites/default/files", "sites/example.com/files");

I can run both of these at the same time.  I first go to my database

Then click the sql tab and run the command in it:

Thats it for the basics.  You may need to do other tables besides these 2 depending on what else you've got going on, but this should help you out.

Imagecache Issues

Imagecache images seem to have some issues also: http://drupal.org/node/697086

To fix this, I had to do something like so:

WARNING, I have not tested this code on D5 (all the above I tested on D6).  But should be similar on D5

UPDATE files set filepath = replace (filepath, 'sites/old.example.com/file/', 'sites/new.example.com/file/');

I had to remove all the sites/example.com/file/ (dont forget the end slash on this one) from all the links.

Test this first

Always do this on a test site first and have backups.  Dont just start going around search replacing on your sites.  That = bad.

Other things to think about

  • site logo
  • images in comments
  • images in things other than comments and nodes

Am I missing anything?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Glad to hear it :).

Helped me out of a big hole, thanks for sharing

[...] to fix my multisite images issues aegir [...]

Post new comment