Portable sites with base href / $base_url in drupal

By declaring a base href you can tell the browser that all relative links contained within the document start from that specified base location. This allows you to view how the related document will look and behave when placed on the server without actually having to do so. It also allows you to put your Drupal site in a subdirectory of your website.

Suppose you have a domain, www.yourdomain.com. You would like to put Drupal in a subdirectory called 'drupal-site'. Your homepage then would be http://www.yourdomain.com/drupal-site.

Now you have an about page, http://www.yourdomain.com/drupal-site/about/ and all your image are under http://www.yourdomain.com/drupal-site/images/. So if you would like to refer to an image in your about page you would have something like:

<img src="/images/myimage.jpg" />

The problem is the browser will interprete this as http://www.yourdomain.com/images/myimage.jpg.

In this case you would state the following tag in your html header:

<base href="http://www.yourdomain.com/drupal-site/" />

Drupal has its own way of working with this. In settings.php you have to put the global variable:

<?php
$base_url = 'http://www.yourdomain.com/drupal-site/';
?>

And in your page.tpl.php you print it:

<?php global $base_url; ?>
<base href="<?php  print $base_url ?>" />
Written on January 22, 2008 at 14:29, tagged as Drupal, theming, things you might have overlooked, tips

Comments

the solutions I found:

  • don't work in subfolders, eg. create subdomains (doesn't help to improve portability)
  • use the l() function . make sure you put the filter to php filter.
  • use one of following modules:
    pathologic or pathfilter

My choice fell on pathfilter, while the l() function would be another option. Only then you give the editor the right to execute PHP...
Pathologic seems quite good. But I didn't want to use server resources unnecessarily. And it seems as pathologic does quite more than just replacing a tag like path filter does.

Hope this helps :)

Search engines and others seem to get a problem with this. It was solved like you describe in older Drupal versions and then changed because of these issues
see http://drupal.org/node/13148#comment-69231

hi Davy.. I discovered yesterday this blog, I think it's very interesting... and like Chris in the previous comment.. I forgot BASE element, so I was so foolish to print base_path() in each internal link! :(

I think that your advice in this post could be furtherly simplified using this code:

" />

so, users may not use the variable $base_url ...

( sorry for my english :P )

I just finished changing all the URLs in our blocks to point to <?php print base_path() ?> but then noticed it wasn't fixing the node variables. How foolish of me! I should have just been changing the <base> tag all along!

I just wasted a lot of time for nothing!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

About

drupalcoder.com is a blog on all things Drupal in specific and LAMP on OS X in general. It is maintained by Davy Van Den Bremt, a Belgian (Drupal) web developer and designer living in Ghent. The goal of this blog is to log all interesting things that have crossed the writer's path while developing Drupal sites. You can read all about Davy's professional activities on his LinkedIn profile. If you want to get in touch, use the contact form.