Steve
Steve

Reputation: 732

Django-hosts redirect form non-www to www

The site generates different URLs that all look like http://example.com/'somepath'. What I want to do is to redirect users from http://example.com/'somepath' to http://www.example.com/'somepath'. As I found out it's possible to do with django-hosts.
As it's said in instructions I have the following in settings.py:

ALLOWED_HOSTS = ['www.example.com', 'example.com']

INSTALLED_APPS = [
   ...,
   'django_hosts',
]

MIDDLEWARE = [
   'django_hosts.middleware.HostsRequestMiddleware',
   ...
   'django_hosts.middleware.HostsResponseMiddleware',
]

ROOT_URLCONF = 'appname.urls'
ROOT_HOSTCONF = 'appname.hosts'
DEFAULT_HOST = 'www'
DEFAULT_REDIRECT_URL = "http://www.example.com"
PARENT_HOST = "example.com"

In hostsconf/urls:

from django.conf.urls import url
from .views import wildcard_redirect

urlpatterns = [
    url(r'^(?P<path>.*)', wildcard_redirect),
]

In hostsconf/views:

from django.conf import settings
from django.http import HttpResponseRedirect

DEFAULT_REDIRECT_URL = getattr(settings, "DEFAULT_REDIRECT_URL", "http://www.example.com")

def wildcard_redirect(request, path=None):
    new_url = DEFAULT_REDIRECT_URL
    if path is not None:
        new_url = DEFAULT_REDIRECT_URL + "/" + path
    return HttpResponseRedirect(new_url)

But looks like it doesn't work because if I go to http://example.com/'somepath' it returns "400 Bad Request" and http://www.example.com/'somepath' points to the correct destination. What am I doing wrong?

Upvotes: 1

Views: 782

Answers (2)

Coding
Coding

Reputation: 1

I was having the same problem.

I was performing local tests on Windows and every time my server only worked on the url www.example.com and not simply example.com.

I realized that the problem itself was not in my Django project but in my OS hosts file, my local DNS. The url example.com in my DNS did not point to the local IP 127.0.0.1 on which the Django application was running.

To solve the problem I had to add example.com and www.example.com as paths to 127.0.0.1 in the appropriate file.

The final files were:


mysite/settings.py:

ALLOWED_HOSTS = ['www.example.com', 'example.com']

INSTALLED_APPS = [
   ...,
   'django_hosts',
]

MIDDLEWARE = [
   'django_hosts.middleware.HostsRequestMiddleware',
   ...
   'django_hosts.middleware.HostsResponseMiddleware',
]

ROOT_URLCONF = 'appname.urls'
ROOT_HOSTCONF = 'appname.hosts'
DEFAULT_HOST = 'www'

mysite/hosts.py:

from django.conf import settings
from django_hosts import patterns, host


host_patterns = patterns('',
    host('', settings.ROOT_URLCONF, name='www'),
    host(r'user', 'User.urls', name='user'),
)

hosts (OS File):

127.0.0.1   localhost
::1         localhost
127.0.0.1   example.com
127.0.0.1   www.example.com
127.0.0.1   user.example.com

To modify your local DNS (Host File). On Linux and macOS, the file to modify is in /etc/hosts. On Windows, the file in question is located in %SystemRoot%\system32\drivers\etc\hosts.

Hope this helps! :)


References:

Django-Hosts Docs

Medium: Django-Hosts Tutorial

Upvotes: 0

dd42
dd42

Reputation: 138

Try the default settings PREPEND_WWW

https://docs.djangoproject.com/en/1.10/ref/settings/#prepend-www

Upvotes: 1

Related Questions