I've added this to my .htacces
:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
but then trying to reach www.example.com
redirects me to:
http://example.com/example/wsgi.py/
because i have WSGIScriptAlias / home/www/example.com/example/wsgi.py
directive in my httpd.conf
and of course i get 404 error.
Eventually, i've managed to fix this by adding next line in my urls.py
:
url(r'^example/wsgi.py/$', index),
(so it redirects to home page)
but i'm not quite sure that this is the right way to do it (because when i try to reach example.com
i see that web browser changes address quickly to www.example.com
and then again to example.com
)
If anyone would ask, yes i've seen this but this didn't help me either, because browser gets url recursive problem (example.com/example.com/example.com/example.com/example.com...)
EDIT : FOLDER STRUCTURE
This is my folder structure:
\mysite\
static\
media\
.htaccess
manage.py
mysite\
templates
templatetags
tinymce
static
urls.py
settigs.py
views.py
wsgi.py
models.py
forms.py
__init__.py
I know this has been answered some time back but to add to the answer given above use
its more readable and also you should use permanent redirect to give the browser correct response header.
Modified version that works with Django 1.11+ style middleware:
This Apache configuration works for me:
I find it much simpler to accomplish no-www redirects with middleware that with with Apache mod_rewrite config.
The middleware that you linked to looks like it does the trick. I'm guessing your problems came from Apache config - make sure you remove all mod_rewrite commands (
Rewrite*
stuff) and then restart the apache server (ref. Apache docs but check for your OS, might be specific).There is only one additional tweak that you should to: make sure you don't redirect any POST requests, because that might result in lost data (tangent ref: Why doesn't HTTP have POST redirect?).
Anyways, this is what I use, worked quite well for me:
To use it, put in a file somewhere, maybe
mysite/mysite/middleware.py
. Then make sure it's run, in yoursettings.py
:If there is no
MIDDLEWARE_CLASSES
in yoursettings.py
then copy the defaults from here in the Django docs but make you're looking at the correct version of Django, there are some changes in 1.7!