File paths is something that i have had issues with on a number of sites when moving a local install (MAMP, OS X) to a remote server.

For example, with a local install as localhost:8888/drupal & remote site as sandbox.example.com/site - when i export my sql tables and before i import them into a new databases (using dreamhost) i remove the /drupal from the paths of images (in blocks, etc). So. an image has the paths

locally - /drupal/files/images/image.jpg
remote - /files/images/image.jpg

What i am finding is that that there problems with the paths.

From my understanding (could be wrong??), if i change $base_url to sandbox.example.com/site (in settings.php) then the path /files/images/image.jpg should work. I am not finding this to be the case. Here are some examples of my fiddling

for the root (sandbox.example.com/site), with file paths as follows:

  • /site/files/images/image.jpg = no problems
  • site/files/images/image.jpg = problem
  • /files/images/image.jpg = problem
  • files/images/image.jpg = no problems

for the non-root pages (sandbox.example.com/site/news), with file paths as follows:

  • /site/files/images/image.jpg = no problems
  • site/files/images/image.jpg = problem
  • /files/images/image.jpg = problem
  • files/images/image.jpg = problem

where problem = broken image (incorrect path)

[I get the same results if i comment out $base_url in settings.php. I ahave also cleared the cache]

I do understand why the image path /files/images/image.jpg is broken. With a $base_url specified in settings.php, should this not work???

What makes things a little more confusing is that files/images/image.jpg = no problems in the root though not in non-root pages.

Anyone able to elaborate on this??

thx
c.

Comments

cog.rusty’s picture

I don't know if you have seen my second reply in this thread: http://drupal.org/node/175427

The same thing seen from a different angle:

files/images/image.jpg is a relative path: The browser attaches it to whatever page path you are currently on.
- When viewing http://example.com/node/35 it will become http://example.com/node/35/files/images/image.jpg, which is wrong.
- When viewing http://example.com -- it is right, but this is just a coincidence.
- When viewing http://example.com/?q=node/35 -- (without clean urls) it is always right, because the "?q=" is not part of the path, it is just an additional query.

/files/images/image.jpg (with a front slash) is an absolute path It has a front slash. This means that the browser will add it directly after the domain name.
- When viewing http://example.com/node/35 -- it is right. It resolves to http://example.com/files/images/image.jpg
- But if you have installed Drupal in a subdirectory, when viewing http://example.com/drupal/node/35 it is wrong again, because it resolves to http://example.com/files/images/image.jpg instead of http://example.com/drupal/files/images/image.jpg. In this case, you need to add the base path /drupal/files/images/image.jpg after the front slash.

So, when using Clean URLs *and* you are moving from a subdorectory to the web root, there is no way to avoid breaking the links if you have entered them manually. You must replace the base path with only a front slash. So, it is a good practice never to give your development sites an URL containing subdirectories. Always make them subdomains, and use a front slash for the image links.

About the $base_url in settings.php. It can't help you with the image links. Normally, your site already works as if it was set. $base_url is there only for cases that your menu links are broken. If the menu links work and you change it to something different, all your menu links will break.

avolve’s picture

thanks for this - i should have searched a bit harder! Knowing it is clean urls makes it easier to understand - that was the biggest issue for me

Will have to rethink how i construct my sandbox sites...

avolve designs | ethical by design

mjlF95’s picture

I ran into this exact same problem and was able to get around it.

Here is a way to look at it. The problem is really that locally your website is in a sub-directory and on the server it is not. If you use a site root relative link it's going to base it on localhost, not localhost/drupal (at least this was the issue I had). I got around this by setting up a virtual host in xampp following the excellent instructions here: http://www.sawmac.com/xampp/virtualhosts/index.php

The sort version is you want to add an entry to your host file, in my case here: C:\Windows\System32\drivers\etc\host. I added "127.0.0.1 dev.example.com.local" to the bottom of the file. This is the URL you'll use (http://dev.example.com.local/) to refer to your local copy.

Then you want to edit C:\xampp\apache\conf\extra\httpd-vhosts.conf and put this at the bottom:

NameVirtualHost *
  <VirtualHost *>
    DocumentRoot "C:\xampp\htdocs"
    ServerName localhost
  </VirtualHost>
  <VirtualHost *>
    DocumentRoot "C:\xampp\htdocs\dev.example.com"
    ServerName dev.example.com.local
  <Directory "C:\xampp\htdocs\dev.example.com">
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

Where the second set of stuff that's in between <VirtualHost *></VirtualHost> is, where "DocumentRoot" and "Directory" are where you have the web site (drupal) files and ServerName is how you want to access them, the same thing you put into the host file.

So once you have all that done (be sure to update your $base_url = in settings.php in the drupal site to reflect the new URL) you'll be able to use site root relative links in you CSS file and these will not change between your local and server copies. Yay!

cog.rusty’s picture

Yes, this is the right thing to do to avoid those problems. You can have any number of local domains without any subdirectry path in their URL.