views:

524

answers:

3

I have a Django site set up that uses the Django admin panel, prefixed with /media/, as well as static site content in a directory called /static/. The admin media stuff, of course, lives within the Django package, and the site's static content is stored along with the Python code for the site.

Currently, my public_html just contains appropriately-named symlinks to the directories that actually hold the static content, as follows:

~/public_html/
    .htaccess
    media -> $HOME/usr/lib/python2.4/site-packages/django/contrib/admin/media/
    site.fcgi
    site -> $HOME/mysite/public/static/

And these are the rewrite rules I'm using in my .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ site.fcgi/$1 [QSA,L]

However, using symlinks in this way feels like a...hack, and it seems like I could use rewrite rules in the .htaccess to avoid using symlinks. (I also want to eventually add a robots.txt file and a favicon file, without having to add -- and maintain -- a bunch of symlinks.) Is this a good idea, and if so, how do I do that?


For future reference, I ultimately did something akin to how Rails projects are generally set up. My Django project contains a public directory, which includes static site content, the FastCGI script, and the .htaccess file, among other things, so it looks sort of like this:

~/
    django/
        public/
            .htaccess
            content/
            favicon.ico
            media@ -> /symlink/to/django/admin/media/
            site.fcgi
            static/

And then, ~/public_html is just symlinked to ~/django/public. Everything works fine, so I'm happy.

+1  A: 

I don't symlink for either...

I use the webserver's rewrites to point /media/(.*) directly to my media directory and /admin\-media/(.*) directly to my admin media. I should stress that these rewrites occur before it gets to the django fastcgi rewrite. I'd give you my code, but I'm using Cherokee instead of Apache.

I don't see why you need to have a "physical" representation in your site root...

Oli
Neither do I, which is why I'm asking how to fix it. ;)
mipadi
I just explained how. Rewrite to their absolute (real) locations rather than symlinking and rewriting to the symlinks.
Oli
I know what to do, just not how to do it correctly using mod_rewrite. I'm not terribly familiar with rewrite rules.
mipadi
In your .htaccess, is it even possible to rewrite to locations outside the document tree?
akaihola
Dreamhost suggests linking in their setup: http://wiki.dreamhost.com/index.php/Django#Media_Files
akaihola
@akaihola: Thanks for the link -- that's insightful, too!
mipadi
+2  A: 

I just use

Alias /media /home/akaihola/usr/lib/python2.4/site-packages/django/contrib/admin/media/
Alias /static /home/akaihola/mysite/public/static/

but I do it in a real Apache configuration file instead of .htaccess and I use mod_wsgi, although I don't know if it makes a difference.

akaihola
I think it does -- from what I understand (from reading the documentation and browsing around online), you can't use mod_alias in a .htaccess file. At any rate, I know my server doesn't allow it, because I got an error about mod_alias when I attempted what you suggested.
mipadi
+3  A: 

Symlinks are not a "hack" - they're legitimate and useful entities in the filesystem. They're no or more less fragile than any kind of file, they reduce redundancy, and reduce the amount of apache configuration you need to do. I'm not quite following why it is you'd want to avoid them. The symlink solution is far more elegant than writing apache rules, IMO.

shacker