Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to make a domain redirection in Django from example.com to www.example.com

I get the following message when I browse to example.com

500 - Internal Server Error

I get my 404 error message when I browse to www.example.com which indicates my site is alive.

How can you make a domain redirection without .htaccess by Django from example.com to www.example.com?

My urls.py

     from django.conf.urls.defaults import *

     from django.contrib import admin
     admin.autodiscover()

     urlpatterns = patterns('',
     # Example:
     # (r'^{{ project_name }}/', include('{{ project_name }}.foo.urls')),

     (r'^home/', include('index.html')),

     # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
     # to INSTALLED_APPS to enable admin documentation:
     (r'^admin/doc/', include('django.contrib.admindocs.urls')),

     (r'^admin/(.*)', admin.site.root),
     )

[edit]

I have the following answer from Djangohosting:

Remove the website proxy for [example.com] and add [example.com] in the aliases section of the [www.example.com] website.

I am not completely sure what it means.

  1. How can you remove the website proxy for example.com?
  2. Where can you find the aliases section of the website www.example.com?
like image 448
Léo Léopold Hertz 준영 Avatar asked Jan 21 '26 13:01

Léo Léopold Hertz 준영


2 Answers

I don't know if it solves all your problems but if you want to do it via django, try a middleware

from django.http import HttpResponseRedirect

class WWWRedirectMiddleware(object):
    def process_request(self, request):
        if not request.META['HTTP_HOST'].startswith('www.'):
            return HttpResponseRedirect('http://www.example.com')

Remember that it must be executed before any other middleware.

Based on this one, didn't test it.

like image 138
zalew Avatar answered Jan 24 '26 12:01

zalew


Checking out PREPEND_WWW setting might help.

like image 43
utku_karatas Avatar answered Jan 24 '26 11:01

utku_karatas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!