Relative Links?

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
lnicks's picture

Hello!

We're trying to develop some new themes for openscholar, and we're having trouble embedding images in tpl.php files. It seems that relative pathing is not working, though static links do. We have to run openscholar on http and https, so static links aren't practical.

The links themselves, though coded correctly, return incorrect results. Is there something causing the links to be changed?

edit: I should add that this issue doesn't occur in our CSS files. It also extends beyond images, so using something like a PHP include, also throws an incorrect path off a relative link, so the include fails to load.

Thanks!

Comments

Relative Links?

dancabral's picture

Hi,

Have you tried a path similar to this one? Without a leading / ? This works successfully in one of our tpl files.

Only local images are allowed.

Best
~Dan Cabral

Relative Links?

dancabral's picture

My apologies--i included the actually tag. This is the path I was referring to "sites/all/themes/custom/scholars_harvard/images/featuredSites.png"

Ok, well we managed to get

lnicks's picture

Ok, well we managed to get the links working by using "/sites/all/themes/custom/scholars_harvard/images/featuredSites.png", with the leading "/", but without it doesn't.

So, problem solved for the moment, but I still kinda want to know why this is happening as well. Any tips on that would be much appreciated.

Thank you!

Relative links and clean URLs

MPetrovic's picture

This happens because browsers get confused on where they are in the server's filesystem when clean urls are enabled.

When you go to an OpenScholar site or really any Drupal site with Clean URLs enabled, you get a URL like this: http://domain.com/vsite/path/to/node
The browser thinks its working directory is exactly that. When you give it a relative link, the browser tries to figure out where the actual file based on its current working directory. So its looking in http://domain.com/vsite/path/to/node/sites/all/files/path/to/image, when it needs to be looking for domain.com/sites/all/files/path/to/image. This is difficult to deal with, because the number of 'directories' the browser thinks its in is completely arbitrary.

Relative links work for css files because the browser is using the css file as its working directory. The location of the css file doesn't change. It's safe.

You have 2 choices in this scenario. The first is to force a relative path from root by starting with /, as Dan suggested. The browser will fill in http://domain.com automatically.

The other is to use the global variable $GLOBALS['base_url'] in place of the domain in your tpl.php files.

I prefer the latter, since the former would break for sites installed in a subdomain. If that's not an issue for you, then its up to you which you pick.

Oh excellent explanation,

lnicks's picture

Oh excellent explanation, thank you very much!

OpenScholar

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: