Login

Fix duplicate first page of paginated results

Author:
muhuk
Posted:
August 31, 2009
Language:
Python
Version:
1.1
Score:
2 (after 2 ratings)

Search engines might conclude there's duplicate content if /some_view/ and /some_view/?page=1 returns the same results. This middleware redirects ?page=1 to the URL without the page parameter. You can set the name of the parameter in settings.py as PAGE_VAR.

See here for more details.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from django.http import HttpResponsePermanentRedirect
from django.conf import settings


PAGE_VAR = getattr(settings, 'PAGE_VAR', 'page')


class FixFirstPaginatedPage(object):
    def process_request(self, request):
        if request.method == 'GET':
            try:
                page = int(request.GET[PAGE_VAR])
            except (KeyError, ValueError, TypeError):
                return None
            if page == 1:
                params = request.GET.copy()
                del(params[PAGE_VAR])
                path = request.path
                if params:
                    path += '?' + params.urlencode()
                return HttpResponsePermanentRedirect(path)

More like this

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

Comments

carljm (on September 1, 2009):

Curious if you have any actual evidence or experience that this is a real problem. I have to think that optional querystring parameters (where supplying the default value gets the same results as supplying no value) are quite common across the web, and search engines must take that into account in their duplicate content algorithms.

#

Please login first before commenting.