Canonical links and 301 Redirect if URL doesn't match slug

I am trying to implement a URL scheme similar to stack overflow's in django/python.

E.g. the pk is stored in the URL along with a slug of the title so for this question (id #4787731) the URL is

https://stackoverflow.com/questions/4787731/canonical-links-and-301-redirect-if-url-doesnt-match-slug

If I later change the title (or just put in some random crud into the url) then the site will still know which question I am after (by the ID) and will 301 redirect to the correct URL - e.g. try.

https://stackoverflow.com/questions/4787731/canonical-links-MODIFIED-URL

So

  • What is the best way to include canonical links in my pages such as

(can I use get_absolute_url)

  • What is the best way to recognize that the current URL doesn't match the canonical link and issue a 301?

Note - This question is similar but only addresses the case of generating the slug on the fly or statically.

8
задан Community 23 May 2017 в 11:52
поделиться