Correct broken links and images on your drupal development environment

I am in the habit of using Backup and Migrate module to quickly move from Live to Local in order to test things out. More than 80000 people trust this module I suppose to keep backups or move from Live to Local and vice versa. One of the problems that might arise when Restoring the database on localhost is that some of the content might contain links and images that will be broken if your drupal installation is in a subfolder which means you have in your .htaccess something like this
RewriteBase /folder/domain.com

and that you access your site from a browser like this
http://localhost/folder/domain.com/

Mind you not all links and images will be broken in the following situation depending on the way they are input in the body areas.

I have tried numerous modules from drupal.org such as search and replace scanner and pathologic but to no avail (probably due to my bad configuration). I decided to find an easy way to solve little mishap that arises quite a lot.

Here is the SQL commands that will do the trick to correct the paths on the src and href attributes of all node bodies and teaser as well as the block bodies

UPDATE `boxes` SET `body` = REPLACE(`body`, 'href="/', 'href="http://localhost/folder/domain.com/');
UPDATE `boxes` SET `body` = REPLACE(`body`, 'src="/', 'src="http://localhost/folder/domain.com/');
UPDATE `node_revisions` SET `body` = REPLACE(`body`, 'href="/', 'href="http://localhost/folder/domain.com/');
UPDATE `node_revisions` SET `body` = REPLACE(`body`, 'src="/', 'src="http://localhost/folder/domain.com/');
UPDATE `node_revisions` SET `teaser` = REPLACE(`teaser`, 'href="/', 'href="http://localhost/folder/domain.com/');
UPDATE `node_revisions` SET `teaser` = REPLACE(`teaser`, 'src="/', 'src="http://localhost/folder/domain.com/');

I am not an SQL guru thus those 6 lines didn't come easy for me thus I am putting them down for anyone that might find them usefull.

Just in case anyone wants to go back from localhost to Live environment the opposite should be run on the live SQL server

UPDATE `boxes` SET `body` = REPLACE(`body`, 'href="http://localhost/folder/domain.com/' ,'href="/');
UPDATE `boxes` SET `body` = REPLACE(`body`, 'src="http://localhost/folder/domain.com/' ,'src="/');
UPDATE `node_revisions` SET `body` = REPLACE(`body`, 'href="http://localhost/folder/domain.com/' ,'href="/');
UPDATE `node_revisions` SET `body` = REPLACE(`body`, 'src="http://localhost/folder/domain.com/' ,'src="/');
UPDATE `node_revisions` SET `teaser` = REPLACE(`teaser`, 'href="http://localhost/folder/domain.com/' ,'href="/');
UPDATE `node_revisions` SET `teaser` = REPLACE(`teaser`, 'src="http://localhost/folder/domain.com/' ,'src="/');

Category: 

2 Comments