Django AND .htaccess rewrites/redirects, is this possible?

梦想的初衷 提交于 2019-12-01 06:13:58

问题


Is it possible to have Apache htaccess rewrites take effect before it hits django?

I want to be able to specify RewriteRules in an htaccess file that take precedence over django, and if nothing matches then it gets dispatched to mod_wsgi/django.

We're using apache2 with mod_wsgi and the apache vhost looks like this:

<VirtualHost *:80>

    DocumentRoot /usr/local/www/site/static

    Alias /css/ /usr/local/www/site/static/css/
    Alias /js/ /usr/local/www/site/static/js/
    Alias /img/ /usr/local/www/site/static/img/
    Alias /flash/ /usr/local/www/site/static/flash/

    <Directory /usr/local/www/site/static>
      AllowOverride All
      Order allow,deny
      Allow from all
    </Directory>

    WSGIDaemonProcess mallorca-site threads=15
    WSGIScriptAlias / /usr/local/www/site/config/dev/dev.wsgi

</VirtualHost>

Thanks


回答1:


First off you are missing WSGIProcessGroup directive. Without that your application isn't going to be running in daemon mode as you possibly expect after having defined WSGIDaemonProcess directive.

Secondly, a .htaccess file isn't even going to be consulted when using WSGIScriptAlias except for the static directories you mapped with Alias directives.

What you need to do is use AddHandler method for mounting WSGI application, with associated rewrite rules to get it to appear at root of site, as documented in:

http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#The_Apache_Alias_Directive




回答2:


You can use an Include to include an access file into your config file, but because the directory structure only exists in urls.py, you wouldn't be able to use a .htaccess file. You can always name your include file .htaccess, but including it inside your VirtualHost is probably the best option.




回答3:


I don't see why you want to do this in an .htaccess file. You should do it in the virtual host configuration you have pasted above.



来源:https://stackoverflow.com/questions/2922545/django-and-htaccess-rewrites-redirects-is-this-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!