play framework2:从 url 中删除尾部斜杠 [英] play framework2: remove trailing slash from urls

查看:25
本文介绍了play framework2:从 url 中删除尾部斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在播放框架 1 中,您可以在路由文件中使用类似这样的内容(查看 http://www.playframework.org/documentation/1.2.5/routes#syntax)

In play framework 1, you could use in the routes file something like this (check documentation at http://www.playframework.org/documentation/1.2.5/routes#syntax)

GET     /clients/?       Clients.index

这样路由将匹配/api/clients 和/api/clients/

so that the route will match /api/clients and also /api/clients/

如何在游戏框架 2 中实现相同的效果?

How can I achieve the same in play framework 2?

推荐答案

从 SEO 的角度来看,带有 尾部斜杠 的相同链接不同于没有它的链接.强烈建议始终使用一种模式(尾随或非尾随链接).

From SEO point of view the same link with trailing slash is other one than link without it. It is highly recommended to always use one schema (trailed or un-trailed links).

虽然有不同的学校,哪个更好,但最重要的是将 301 重定向从错误"的 URL 重定向到正确的 URL.您可以使用跨越多个/ 的动态部分在 Play 中轻松实现.

Although there are different schools which one is better the most important is to make a 301 redirect from 'wrong' URL to the correct. You can achieve it quite easy in Play with a 'Dynamic part spanning several /'.

我个人更喜欢无尾版,可能是因为在 Play 中实现它就像写几行简单的代码.将此规则添加到路由文件的开头(保留斜杠 - 这很重要,因为它不被视为跨组中的下一个斜杠,并允许轻松匹配尾随 URL):

Personally I prefer un-trailed version, maybe because implementing it in the Play is just like writing few simple lines. Add to your routes file this rule, somewhere at the beginning (keep the slash - it's important as it's NOT considered as next slash in the spanning-group, and allows to match trailed URL's easily):

GET  /*path/  controllers.Application.untrail(path: String)

然后你可以在控制器中进行重定向 - 到参数,所以它最后没有斜线:

then you can just make a redirect in the controller - to the param, so it will be without the slash at the end:

Java

public static Result untrail(String path) {
   return movedPermanently("/" + path);
}

Scala

def untrail(path: String) = Action { 
  MovedPermanently("/" + path)
}

直到现在,所有以斜线结尾的路由都会被重定向到无尾版本.简单:)

Until now, all routes ending with the slash will be redirected to the un-trailed version. Easy :)

当然,强烈建议使用反向路由器来生成正确的 URL - 以最小化冗余重定向.此外,如果您在某处(即在某些 JS 或外部应用程序中)硬编码 URL,最好编写正确的 URL,而不是每次都转换它们.如果您计划发布一些公共 API,请在文档中注明您的应用程序更喜欢哪种模式,这样开发人员就会收到警告,并且(可能)会准备正确的调用.

Of course it's highly recommended to use reverse router for generating correct URL's - to minimalize redundant redirects. Also if you're hardcoding the URL somewhere (ie. in some JS or in external application) it's also better to write correct ones instead converting them every time. If you're planning to publish some public API make a note in documentation, which pattern does your application prefer, so developers will be warned and (maybe) will prepare correct calls.

更重要的是 - GET 路由最重要,因为它们容易受到客户端的操纵.在使用 POSTPUTDELETE 和其他你不需要(或者更确切地说,你不应该)关心重定向时用户无法更改,因此您需要记住您选择的方式.在错误呼叫的情况下,即.对于 POST,只需返回 404 错误 - 因此第三部分应用程序的开发人员将有义务使用正确的结尾.

What's more - it most important for GET routes as they are a subject to manipulation from the client's side. While using POST, PUT, DELETE and others you don't need (or rather, you should't) to care about redirects as they can not be changed by the user and in that way you need to remember which way you choose. In case of wrong call ie. for POST, just return a 404 error - so the developer of the 3-rd part application will be obligated to use correct endings.

这篇关于play framework2:从 url 中删除尾部斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆