Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide canonical URL with Django HttpResponseRedirect?

This question is very similar to one I just asked href: Can I get Google search results to use/display the final redirect url?, but now the question is specific to Django.

My site has webpage urls that use the following format:

www.mysite.com/id/pretty_title

The front page links to these pages, but the href actually contains some parameters:

 www.mysite.com/id/?some_ugly_parameters_to_let_me_know_what_search_it_is_from

This then redirects to

www.mysite.com/id/pretty_title

which shows the page.

My issue is that Google's search results show the link to the page as the ugly url instead of the pretty redirected one.

What I have learned is that I need to provide a canonical link. But how can I do this when the ugly url page never really exists, at least not as one that I have written?

What happens server side is that the view of the ugly url does a redirect:

return HttpResponseRedirect(pretty_url)
like image 828
user984003 Avatar asked Mar 29 '14 08:03

user984003


People also ask

How do I create a canonical URL?

Navigate to the Settings tab. Then click Advanced Options. In the Canonical URL section, enter a canonical URL for the page or post's content. This is the URL search engines will prioritize when displaying search results relevant to this content.

What is canonical URL in Django?

Canonical url is a url that tell search engine ( google, bing etc ) which url is the primary url for same content web pages. It is used to fix the duplicate content issue with different webpage url formats. So that search engine will know all the url that generate same web content is belongs to one canonical url.

What is HttpResponseRedirect in Django?

HttpResponseRedirect is a subclass of HttpResponse (source code) in the Django web framework that returns the HTTP 302 status code, indicating the URL resource was found but temporarily moved to a different URL. This class is most frequently used as a return object from a Django view.

How do I find my canonical URL?

A canonical URL is the URL of the page that Google thinks is most representative from a set of duplicate pages on your site. For example, if you have URLs for the same page ( example.com?dress=1234 and example.com/dresses/1234 ), Google chooses one as canonical.


2 Answers

I think this is the correct built template tag that you're looking for. {{ request.build_absolute_uri }}

like image 64
Alex Phelps Avatar answered Oct 19 '22 22:10

Alex Phelps


You can just put it as part of the HTML returned from the Django template, in the <head> section. Do you have a base.html in your Django? You can setup a {% block %} as a placeholder for the canonical URL and then set that value in each individual page that {% extends base.html %}

base.html

<html>
<head>
  <link rel="canonical" href="{% block canonical_url %}{% endblock %}">
</head>
...
like image 6
bakkal Avatar answered Oct 19 '22 21:10

bakkal