Skip to content

Serving favicon in an Django App using Apache

2008 August 13
tags:
by pk

I got a free few minutes to work on my own site here. Since I migrated the site from all static pages to Django served, I still haven’t put back the favicon icon back onto the site. The sites runs under a virtual host in apache2 at WebFaction. This is what you need to put in your httpd.conf file:

alias /favicon.ico /home/your-home/your-app-etc/static/image/favicon.ico

<LocationMatch "\.(jpg|css|gif|pdf|ico)$">
SetHandler None
</LocationMatch>

The alias line tells apache to go look for the favicon.ico file at a static location of your choice.

The LocationMatch directives tell apache to not run those files thru the Django engine.
Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Posterous
  • Tumblr
  • Twitter
7 Responses leave one →
  1. November 2, 2008

    Hey there. I tried to do this following your instructions, but it’s not working for me. I put the appropriate lines in httpd.conf and restarted apache.

    Do I have to do something else besides update httpd.conf? You can email me back if you are inclined at andrewljohnson [at] gmail.com

  2. January 1, 2009

    I’m using FastCGI for all my django sites and there’s an even simpler solution: Adding the following does the trick:

    RewriteRule ^/favicon.ico$ /media/favicon.png [QSA,L,PT]

  3. January 6, 2009

    hey. even easier:

    if you have django.redirects installed, just write a new redirect!

  4. March 22, 2009

    Is it not easier to add the favicon via a tag in the html?

    I’m currently doing this in my websites, and since no-one mentions it, I’m afraid its a bad use?

  5. pk permalink*
    March 22, 2009

    The idea here is to by pass Django and just make the apache web server serves up the fav icon. Otherwise the other’s suggestions work also, but more work for Django.

    Andre — the favicon tag is still needed in the HTML. This is just to deal with what happens when the browser ask for the fav icon file.

  6. June 12, 2009

    Another option, one that doesn’t require knowing the full path to this particular instance of django is to use another location directive.

    So I have first, a location directive for “/” that sets the handler to django

    Then one for “/favicon.ico” that sets the handler to none, and finally one for “/media” which also sets the handler to none

    This follows the same style as the “serving media files” section of http://docs.djangoproject.com/en/dev/howto/deployment/modpython/#id1

  7. January 17, 2011

    For nginx you can use:
    If you’re using nginx, you can add the line:
    location = /favicon.ico {
    rewrite (.*) /media/favicon.ico;
    }
    in your .conf

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS