播放framework2:从网址中删除结尾的斜杠 [英] play framework2: remove trailing slash from urls

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

问题描述

在播放框架1中,您可以在路由文件中使用类似的内容(请查看

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框架2中实现相同目标?

How can I achieve the same in play framework 2?

推荐答案

从SEO角度来看,具有trailing slash的同一链接不同于没有链接的相同链接.强烈建议始终使用一个架构(已链接或未链接的链接).

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错误-因此,第3部分应用程序的开发人员必须使用正确的结尾.

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:从网址中删除结尾的斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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