Bulletproof favicon on multisite server

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Forum Members
Forum Members
Posts: 27
Joined: Fri Dec 10, 2010 5:35 pm

Bulletproof favicon on multisite server

Post by king_cucaracha »

Do you have a multi site server && PRETTY_URLS && Favicon on different location than CMSMS index.php

In my case I have
  • favicon.ico on uploads/misc directory
  • Test on WAMP server (multisite server)
  • Deploy on a CPanel shared hosted for only one site per account
  • Pretty_URLS Active on both TEST and PRODUCTION
  • Some sites need flat pretty_urls some sites don't
  • Use the favicon UDT
That's a perfect recipe for favicon mess


If you use relative paths over non flat pretty_urls you will have the path to favicon relative to current content level on content tree
If you use flat pretty_urls your client might want tomorrow to change to non-flat pretty_urls.

The solution might be to use absolute path for favicon, that works ok on single site shared host, but if you try to use absolute path on multisite server you may end up with absolute paths pointing to your /home directory.

So, sometimes you may have favicon working on start page and not working on rest of site, or not working at all. Or favicon working on production site, but not on test server.
You can change the paths to favicon on test site, and then rearrange them to work on production, but if you use more than a couple of templates it might become a PITA on the long run.

The solution is to use the {root_url} tag for creating dynamic absolute paths (sounds weird uh. Always tought dynamic and absolute are mutually excluding) inside the {favicon} udt

Since you can't call to {root_url} inside the {favicon} udt. And like Calguy says "Smarty is smart but not that smart" (talking about nested smarty tags on another thread in this forum) I took his approach and got to this:

Code: Select all

{capture assign='root_url'} {root_url}{/capture}
{favicon static="$root_url/uploads/misc/favicon.ico"}
the first line, takes the value from {root_url} tag, converts it to an smarty variable and passes it to the second line and there it is. Now you have dinamically created an absolute url to your site favicon.

the result will be this line generated on your html

Code: Select all

<link rel="shortcut icon" href=" http://yourserver/CMSMS_ROOT/uploads/misc/waterland.ico" type="image/x-icon" />
Correct every time

Hope this is helpful and avoid some headaches to some not so experienced webmasters (like myself).


Return to “Tips and Tricks”