创建包含 id 和 title slug 的规范 URL
我想复制 StackOverflow 对其 URL 所做的事情。
例如:
或
C# 的隐藏功能C#? - (C# 的隐藏功能?)
将带您到同一页面,但当他们返回到浏览器总是返回第一个。
如何实施更改以便返回更大的 URL?
I want to replicate what StackOverflow does with its URLs.
For example:
Hidden Features of C#? - (Hidden Features of C#?)
or
Hidden Features of C#? - (Hidden Features of C#?)
Will Take you to the same page but when they return to the browser the first one is always returned.
How do you implement the change so the larger URL is returned?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我之前处理这个问题的方法是有两个路由,
现在在控制器操作中按此顺序注册,
如果我们只有身份证号。我们还通过对照问题标题的 slugged 版本进行检查,确保传递的标题是正确的,从而为包含 id 和正确标题 slug 的问题创建规范 URL。
使用的几个助手
The way that I've handled this before is to have two routes, registered in this order
now in the controller action,
We'll permanently redirect to the URL that contains the title slug (lowercase title with hyphens as separators) if we only have the id. We also make sure that the title passed is the correct one by checking it against the slugged version of the question title, thereby creating a canonical URL for the question that contains both the id and the correct title slug.
A couple of the helpers used