How do I go about redirecting all requests for domain.com/... to www.domain.com/... with a 301 in a django site?
Obviously this can't be done in urls.py because you only get the path part of the URL in there.
I can't use mod rewrite in .htaccess, because .htaccess files do nothing under Django (I think).
I'm guessing something in middleware or apache conf?
I'm running Django on a Linux server with Plesk, using mod WSGI
This also can be done with a middleware.
Some examples:
This is a better version of snippet-510:
I've tried Graham's solution but can't get it to work (even if the simplest case of http (not https) without any path. I'm not experienced with wsgi as you can probably guess and all help is deeply appreciated.
Here's my attempt (redirecting from www.olddomain.com to www.newdomain.com). When I try to deploy it, trying to reach www.olddomain.com results in a error ("Can't reach this page"):
Thank you for your help
The WebFaction discussion someone pointed out is correct as far as the configuration, you just have to apply it yourself rather than through a control panel.
Put in .htaccess file, or in main Apache configuration in appropriate context. If inside of a VirtualHost in main Apache configuration, your would have ServerName be www.example.com and ServerAlias be example.com to ensure that virtual host handled both requests.
If you don't have access to any Apache configuration, if need be, it can be done using a WSGI wrapper around the Django WSGI application entry point. Something like:
Fixing this up to include the URL within the site and dealing with https is left as an exercise for the reader. :-)
There is a lightweight way to do that involving VirtualHosts and mod_alias Redirect directive. You can define two VirtualHosts, one holding the redirect and another holding the site configuration:
And that will do the job.
The PREPEND_WWW setting does just that.
A full thread about the issue exists here http://forum.webfaction.com/viewtopic.php?id=1516