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

查看:18
本文介绍了播放 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/

如何在 play framework 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).

虽然有不同的学校哪个更好,但最重要的是从错误"的 URL 到正确的 URL 进行 301 重定向.您可以在 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.

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

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