问题描述
这个问题与我刚问过的一个问题非常相似,但现在的问题只针对Django。
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
首页链接到这些页面,但href实际上包含一些参数:
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
然后重定向到
www.mysite.com/id/pretty_title
显示页面。
我的问题是 Google的搜索结果显示页面链接为丑陋的URL,而不是经过重定向的URL。
My issue is that Google's search results show the link to the page as the ugly url instead of the pretty redirected one.
我了解到的是,我需要提供一个规范的链接。但是,当丑陋的url页面从未真正存在时,至少不像我所写的那样,该怎么办?
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)
推荐答案
您可以将其作为Django模板返回的HTML的一部分,放在<头>
部分。
您的Django中是否有 base.html
?您可以将 {%block%}
设置为规范URL的占位符,然后在 {%扩展基数的每个页面中设置该值。 html%}
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
base.html
<html>
<head>
<link rel="canonical" href="{% block canonical_url %}{% endblock %}">
</head>
...
这篇关于如何使用Django HttpResponseRedirect提供规范的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!