Serving favicon in an Django App using Apache

by pk on August 13, 2008

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/Bookmark

{ 6 comments… read them below or add one }

Andrew November 2, 2008 at 10:55 pm

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

and3k January 1, 2009 at 11:14 am

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]

Kyle January 6, 2009 at 10:54 pm

hey. even easier:

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

Andre March 22, 2009 at 12:27 pm

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?

pk March 22, 2009 at 6:10 pm

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.

KarlP June 12, 2009 at 5:41 am

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

Leave a Comment

Previous post: Resetting Django Admin Password

Next post: 3 year old is programming Scratch sprite