Search code examples
djangoapache.htaccessno-www

Django - remove www from URLs


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


Solution

  • 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:

    from django.http import HttpResponseRedirect
    
    class NoWWWRedirectMiddleware(object):
        def process_request(self, request):
    
        if request.method == 'GET':  # if wanna be a prefect REST citizen, consider HEAD and OPTIONS here as well
            host = request.get_host()
            if host.lower().find('www.') == 0:
                no_www_host = host[4:]
                url = request.build_absolute_uri().replace(host, no_www_host, 1)
                return HttpResponseRedirect(url)
    

    To use it, put in a file somewhere, maybe mysite/mysite/middleware.py. Then make sure it's run, in your settings.py:

    MIDDLEWARE_CLASSES = (
        'mysite.middleware.NoWWWRedirectMiddleware',
        # ... other middleware ...
    

    If there is no MIDDLEWARE_CLASSES in your settings.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!