views:

394

answers:

4

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

500 - Internal Server Error

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

How can you make a domain redirection without .htaccess by Django from site.com to www.site.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 [site.com] and add [site.com] in the aliases section of the [www.site.com] website.

I am not completely sure what it means.

  1. How can you remove the website proxy for site.com?
  2. Where can you find the aliases section of the website www.site.com?
+2  A: 

The 500 Internal Server error is likely caused by this line:

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

The include() function is a way to include other URL config files, not static HTML files. You'd have to either serve index.html as a static file at the given URL (/home), or write a view that simply renders the file.

mipadi
I commented the line out. The same error occurs. @mipadi: Thank you for pointing that out!
Masi
+2  A: 

Checking out PREPEND_WWW setting might help.

utku_karatas
The problem is that you wouldn't be able to find the site for the prepend_www setting to work. :-)
Jason Baker
+1  A: 

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

500 - Internal Server Error

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

It's likely the other way around. The 500 message means that your site is active, but giving an error. What you have to understand is that the site.com/www.site.com part of the url serve to find which server to connect to. By the time your django app gets called, this part has already been completed, so there's no way to do this from django.

Instead, you want to set up www as a subdomain of site.com and make them point the same place. See the first example here: virtualhosts examples

UPDATE I just noticed that you stated that your 404 was showing up, so ignore the first two sentences. Either way, the solution would likely be the same. :-)

Jason Baker
+2  A: 

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.mysite.com')

Remember that it must be executed before any other middleware.

Based on this one, didn't test it.

zalew