Login

Redirect Multiple Domains to a Single Domain

Author:
rpoulton
Posted:
October 2, 2007
Language:
Python
Version:
.96
Score:
0 (after 0 ratings)

Often, you may register more than one domain name for your website, which may have a primary domain of mysite.com.au:

  1. mysite.com
  2. my-site.com
  3. mysite.net
  4. mysite.co.uk

For SEO and brand awareness reasons, (remember: every page should have exactly one URL) you want every visitor to end up on your primary domain, mysite.com.au.

This middleware checks the HTTP_HOST for all incoming requests, and sends the user to http://www.mysite.com.au/ if they've managed to hit another domain.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from django.http import HttpResponseRedirect

class ValidateHostMiddleware(object):
    """
    In Apache's httpd.conf, you may have ServerName set to mysite.com.au along 
    with a number of aliases: mysite.com, mysite.net, my-site.com etc.

    This middleware redirects any request that isn't for mysite.com.au to that 
    domain, helping with SEO and brand recognition.
    """
    def process_request(self, request):
        if not request.META['HTTP_HOST'].endswith('mysite.com.au'):
            return HttpResponseRedirect('http://www.mysite.com.au/')

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 9 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 10 months, 3 weeks ago

Comments

Please login first before commenting.