如果 URL 与 slug 不匹配,则规范链接和 301 重定向

发布于 2024-10-14 01:28:51 字数 890 浏览 6 评论 0原文

我正在尝试在 django/python 中实现类似于堆栈溢出的 URL 方案。

例如,pk 与标题的 slug 一起存储在 URL 中,因此对于这个问题(id #4787731),URL 是

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

如果我稍后更改标题(或者只是在 url 中放入一些随机的内容),那么该网站仍然会知道我要问哪个问题(通过 ID)并将 301 重定向到正确的 URL - 例如尝试。

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

那么

  • 在我的页面中包含规范链接的最佳方式是什么,例如

(我可以使用 get_absolute_url)

  • 识别当前 URL 与规范链接不匹配并发出 301 的最佳方法是什么?

注意 - 这个问题类似,但仅解决 动态或静态生成 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

    <link rel="canonical" href="https://stackoverflow.com/questions/4787731/canonical-links-and-301-redirect-if-url-doesnt-match-slug">

(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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏夜暖风 2024-10-21 01:28:51

1:我认为如果存在 301,那么使用规范标签就没有意义。

让我们想象一下将 URL 从 /q/111/hello-world 更改为 /q/111/foobar 的场景。引擎不会假设两者相等,除非它们访问带有规范标记的原始 url,指向 /q/111/foobar (它不会,因为它现在是 301,切断任何页面之间关系的证明)。

2:我会用直接的方式来做。定义一个非唯一的 slug 字段并与详细视图中捕获的 URL 进行比较。

# models
class MyModel(models.Model):
    # ...
    non_unique_slug = models.SlugField()

    def get_absolute_url(self):
        return "/questions/%s/%s" % (self.id, self.non_unique_slug)


# urls
    r'^questions/(?P<id>\d+)/(?P<slug>[\w-]+)/
 

# views
def my_view(request, id, slug):
    page = Page.objects.get(id=id)
    if not slug == page.slug:
        return http.HttpResponsePermanentRedirect(page.get_absolute_url())

    # render page
    return direct_to_template(request, "foobar.html", {'page': page})

1: I don't think there's a point in using the canonical tag if there are 301s anyways.

Let's just imagine a scenario where you change the URL from /q/111/hello-world to /q/111/foobar. The engines won't assume the two are equal unless they visit the original url with the canonical tag on it pointing to /q/111/foobar (which it wont, because it's now a 301, severing any proof of a relationship between the pages).

2: I'd do it the straight forward way. Define a non unique slug field and compare vs the captured URL in your detail view.

# models
class MyModel(models.Model):
    # ...
    non_unique_slug = models.SlugField()

    def get_absolute_url(self):
        return "/questions/%s/%s" % (self.id, self.non_unique_slug)


# urls
    r'^questions/(?P<id>\d+)/(?P<slug>[\w-]+)/
 

# views
def my_view(request, id, slug):
    page = Page.objects.get(id=id)
    if not slug == page.slug:
        return http.HttpResponsePermanentRedirect(page.get_absolute_url())

    # render page
    return direct_to_template(request, "foobar.html", {'page': page})
狼性发作 2024-10-21 01:28:51

我按照 Yuji 的有用说明进行操作,但发现您需要使用 HttpResponsePermanentRedirect 对象来获取永久 301 而不是临时 302。

I followed Yuji's helpful instructions but found that you'll need to use the HttpResponsePermanentRedirect object to get a permanent 301 instead of the temporary 302.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文