DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 12.8 Setting Up a Default "Favicon"

Problem

You want to define a default favorite icon, or "favicon," for your site, but allow individual sites or users to override it.

Solution

Put your default favicon.ico file into the /icons/ subdirectory under your ServerRoot, and add the following lines to your server configuration file in the scope where you want it to take effect (such as inside a particular <VirtualHost> container or outside all of them):

AddType image/x-icon .ico
<Files favicon.ico>
    ErrorDocument 404 /icons/favicon.ico
</Files>

Discussion

favicon.ico files allow web sites to provide a small (16 x 16 pixels) image to clients for use in labeling pages; for instance, the Mozilla browser will show the favicon in the location bar and in any page tabs. These files are typically located in the site's DocumentRoot or in the same directory as the pages that reference them.

What the lines in the solution do is trap any references to favicon.ico files that don't exist and supply a default instead. An ErrorDocument is used instead of a RewriteRule, because we want the default to be supplied only if the file isn't found where expected. A rewrite, unless carefully crafted, would force the specified file to be used regardless of whether a more appropriate one existed.

See Also

    [ Team LiB ] Previous Section Next Section