views:

43

answers:

3

I'm working on a drupal 6 site at mydomain.com/drupalsite, and the designer has put a lot of hardcoded image paths in there, for instance a custom img folder in mydomain.com/drupalsite/img. So a lot of the site uses links to /drupalsite/img/myimg1.png.

Here's the problem -- the site is eventually moving to finaldomain.com, via pointing finaldomain.com to mydomain.com/drupalsite. So now paths like /drupalsite/img/myimg1.png will resolve to finaldomain.com/drupalsite/img/myimg1.png, instead of what should be finaldomain.com/img/myimg1.png. The finaldomain.com site has to point to that subdirectory so it hits the index.php.

My first instinct is to use an .htaccess file to replace the /drupalsite with "", but I've tried about a dozen different solutions and they haven't worked. My hack of a solution was to use some ln -s links but I really don't like it :) tia

Andrew

A: 

I've done this before by taking a full database dump, opening it in a text editor, and doing a global search and replace on the paths. Then on the new host, load the modified dump file, and it will have the correct paths in.

Andy Mortimer
In my case I'd have to search/replace files too like page-front.tpl.php etc. I've done this approach in the past and just figured there's a better way...
Andrew
Fair enough. Mine was a fairly simple site, fortunately, and everything did happen to be stored in the database.
Andy Mortimer
A: 

You could try Pathologic, it should be able to correct paths like this.

Fabian
Andrew
It is an input filter, it won't work on template files.
Fabian
+2  A: 

The best method, in hindsight, is to ensure folks use Drupal functions to make all links:

  • l (that's the letter L)
  • drupal_get_path()
  • base_path()

The l() function takes care of base path worries, and provides a systematic way to define your URL's. Using things like theme_image() plus the l() function are a sure win. Use the second and third functions above if you have to write your own <a> tags and for use inside theme functions like theme_image().

But for your current situation:

As regards Andy's solution, it would be better if you could limit your changes to certain database fields where you know the links are located.

So write a query to select all those fields (e.g. all body fields):

$my_query = db_query("SELECT vid, body FROM {node_revisions}");

This, for example, will get you every body field in the node_revisions table, so even your old revisions would have proper links.

Then run through those results, do str_replace() on each, and then write the changes back:

while($node = db_fetch_object($my_query)) {
  $new_body = str_replace('what you have', 'what you want', $node->body);
  db_query("UPDATE {node_revisions} SET body = '%s' WHERE vid = %d", $new_body, $node->vid);
}

I'd obviously try it on one record first, to make sure your code behaves as intended (just add a WHERE vid = 5, for example, to narrow it down to one revision). Furthermore, I haven't taken advantage of node_load and node_save, which are better for loading and saving nodes properly, so as to provide a more general solution (for you to replace text in blocks, etc.).

For your files, I'd suggest a good ol' sed command, by running something like the following from within your "sites" folder:

find ./ -type f -exec sed -i ’s/string1/string2/’ {} \;

Nabbed that from here, so take a look on that site for more explanation. If you're going to be working with paths, you'll either need to escape the / of the paths in your version of the sed command, or use a different sed separator (i.e. you can write s#string1#string2# instead of s/string1/string2/, so you could write s#/drupalsite/img/#/img# instead of s/\/drupalsite\/img\//\/img/ :-). See also Drupal handbook page for quick sed commands: http://drupal.org/node/128513.

A bit of a mess, which is why I try to enforce using the proper functions up front. But this is difficult if you want themers to create Drupal content but you don't want to give them access to the "PHP Filter" input format, or they simply don't know PHP. Proper Drupal theming, at any point past basic HTML/CSS work, requires a knowledge of PHP and Drupal's theme-related functions.

semperos
+ 1 for the reminder to use the right Drupal functions.
kiamlaluno
I initially tried to get her to go that route using base_path(), but she uses like dreamweaver or something on a local computer, which doesn't jive too well with drupal, leaving me the nice task of cleaning it all up :)
Andrew
I completely understand. I don't think I've worked on one Drupal site where I didn't have to fix broken links for these very reasons. Let us know if we can help further.
semperos