Ad

Django: Identify The Urls That Provide Duplicate Content And Set A Canonical Link

- 1 answer

models.py

class People(models.Model):
    name = models.CharField(max_length=16)
    meters_away = models.IntegerField()

Lets populate the db:

>>> from people.models import People
>>> a = People()
>>> a.name = 'George'
>>> a.meters_away = 15
>>> a.save()
>>> b = People()
>>> b.name = 'Jim'
>>> b.meters_away = 10
>>> b.save()

Supposing that we have a url that returns all people in a range of x meters:

http://example.com/range/<meters>

This url scheme accepts 3 hits as follows:

  1. http://example.com/range/20
  2. http://example.com/range/30
  3. http://example.com/range/40

Those hits will create the following queries:

>>> hit1 = People.objects.filter(meters_away__lt=20)
>>> hit2 = People.objects.filter(meters_away__lt=30)
>>> hit3 = People.objects.filter(meters_away__lt=40)

Where:

>>> list(hit1) == list(hit2) == list(hit3)
>>> True

This means that example.com, will serve 3 different urls with the same content.

From a SEO point of view, how could all the possible urls (meters: 21, 22, 23, 24, 30, 40 etc) be filtered in a way that a canonical url is appended to them?

Ad

Answer

The way i understood your question, you may want to get the maximum distance in meters that produce the same result as the current distance (say m meters):

next_number = People.objects.filter(meters_away__gte=m).order_by('meters_away')[:1]
next_number = next_number[0] if next_number else m

and the canonical url will be:

http://example.com/range/<next_number>

Ad
source: stackoverflow.com
Ad