2

I have a drupal site at http://example.com and a development version of the same site at http://another.example.com/subdir. When I'm working on the development version, content that references /sites/default/files/ - such as images - doesn't work because it needs to include the '/subdir' part of the path. Setting $base_url doesn't work because that uses the BASE element's href attribute which doesn't affect urls beginning / if the href includes a subdirectory.

What's the best way of including the subdir in the image references? Do I need to resort to a server rewrite?

3 Answers 3

4

There are two things you can do here in order to try and make you site more portable.

In your templates:

  • Always use l() for all links (even JavaScript links)
  • If you need to reference an image in the theme use:

    <img src="<?php echo $base_path . drupal_get_path('theme', 'themename'); ?>/logo.png" />
    

In your content:

  • You do not really have a way in order to preserve uploaded content URL's unless using some form of file manager which do not create absolute links, but much rather links that get sent to drupal that redirect to the file
  • If the above is not going to work for you then a modification of the rewrite rules for the drupal site sitting under the subdir is required to basically rewrite all URLs requested that start with sites/default/files/(.*) rewritten to subdir/sites/default/files/$1

And the apache mod_rewrite rule may look like the following.

# Redirect all files requests to a subdir
RewriteRule ^/?sites/default/files/(.*) /subdir/sites/default/files/$1 [R=301,L]
1
  • Yup - the rewrite turned out to be the best way of doing this - thanks.
    – Bobby Jack
    Mar 9, 2011 at 10:46
1

If you have any flexibility over the domain of development site at all, we've found that everything just works if you use:

example.com -> production

dev.example.com -> development

Your sites dir will work as /sites/example.com in both cases. You just need to modify your base_url to match the domain.

1

The module Pathologic can help you with this.

1
  • 1
    Indeed, this is pretty much exactly the use case I wrote it for. :D May 21, 2011 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.