Thursday, January 17, 2008

Media data without media directory

It's not a big trouble, but I wanted to remove the /media/ prefix on all my media links, like...


<img alt="My Image" src="/media/img/myimage.png"/>
<link rel="stylesheet" href="/media/css/mysheet.css" type="text/css"/>


and so on.

This can be easily achieved by replacing in apache's configuration file:


<Location "/media/">
SetHandler None
</LocationMatch>


by


<LocationMatch "/((css|js|img|swf|pdf)/|favicon.ico)">
SetHandler None
</LocationMatch>


Of course you have to have all you media files in folders like css, js, img... or anything that you specify in last regular expression.

UPDATE: See this post before using this approach.

6 comments:

  1. I'm pretty sure that they give an example in the docs on the Django site where they use something like the following:


    SetHandler None


    Which will snag any file with an extension that you've listed and stop mod_python from serving it .... much simpler than sorting your pngs and gifs into their own files : )

    ReplyDelete
  2. Hmmm .... your comment system ate my config (must have thought that it was html)

    I had the following:

    LocationMatch "\.(jpg|css|png|gif|js)$"

    inside the tags, which is capturing the file extension of whatever is being served (rather than the folder).

    ReplyDelete
  3. I haven't seen it in the documentation, may be it's in the django book.

    Anyway, I prefer to set it by directory. May be I have an image in my django directory that I don't want to share.

    Thanks anyway for your comment, it's always good having more opinions, ideas...

    ReplyDelete
  4. Admin, please send me mail at ninoksa@gmail.com, thanks.

    ReplyDelete
  5. Thanks for the tip!

    ReplyDelete
  6. That was a well written article, very intereting,thank you for a good read.

    ReplyDelete