在 Grails 中从一个 Web 域重定向到另一个 Web 域的最佳实践?
我目前正在 Grails 中开发一个过滤器,它允许我将 foo.org 上的所有传入请求重定向到 foo.com 上的同一子页面。 到目前为止,我一直在执行以下操作:
if(!(""+request.requestURL).toLowerCase().startsWith(
grailsApplication.config.grails.serverURL ))
{redirect(url:"${grailsApplication.config.grails.serverURL}${request.requestURI}",params:params) }
不幸的是,我在这种方法中遇到了几个问题:
- request.requestURI 值的行为似乎与预期不同:而不是给我正常的“/[controller]/[action]”模式我期望它返回类似:“/grails/[controller]/[action].dispatch”。 - 是否有其他方法来获取“正常”URI? (请原谅,如果这是微不足道的,但无法在文档中找到它,也无法尝试请求对象上可用的各种方法)
- 参数没有在上面的重定向中传递。这可能是由于我在重定向中使用“url”参数,根据文档,该参数应该用于重定向到绝对路径(这又导致它忽略 params 部分?)。但是,由于在重定向到另一个域时我将无法使用正常的重定向(控制器:...,操作:...)方法,因此我可以使用什么方法将参数正确传递到 foo 上的子页面.com?正在考虑一种解决方案,我将通过 params.each{} 闭包手动添加参数,但是是否有更优雅的解决方案?
- 301 重定向。由于我的重定向具有永久性,因此我想使用 301 状态代码。我尝试设置“response.status = 301”,但在使用 Grails redirect(...) 方法时它似乎被忽略。此外,我可以从 grails.org 看到这似乎是在 grails 2.0 中引入的,但是现在有没有办法在 Grails 1.3.7 中获得它?
I am currently working on a filter in Grails that will allow me to redirect all incoming requests on foo.org to the same subpage on foo.com.
So far I have been doing the following:
if(!(""+request.requestURL).toLowerCase().startsWith(
grailsApplication.config.grails.serverURL ))
{redirect(url:"${grailsApplication.config.grails.serverURL}${request.requestURI}",params:params) }
Unfortunately, I am experiencing several issues in this approach:
- The request.requestURI value seems to behave differently than expected: instead of giving me the normal "/[controller]/[action]" pattern as I would expect, it returns something like: "/grails/[controller]/[action].dispatch". - Is there an alternative way to obtain the "normal" URI? (excuse me if this is trivial, but have not been able to find it in the documentation, nor by trying out the various methods available on the request object)
- Params are not being passed in the above redirect. This is probably due to the fact that I am using the "url" parameter in the redirect which according to the docs is supposed to be used for redirects to absolute paths (which again causes it to ignore the params section?). However, since I will not be able to use the normal redirect(controller:...,action:...) approach when redirecting to another domain what approach could I use in order to pass the params correctly along to the subpage on foo.com ? Am considering a solution where I will add the params manually via a params.each{} closure, but isn't there a more elegant solution to this?
- 301 redirects. Since my redirects are of a permanent nature, I would like to use the 301 status code. I have tried to set "response.status = 301" but it seems to be ignored when using the Grails redirect(...) method. Further I can see from grails.org that this seems to be introduced with grails 2.0, but is there a way to obtain this already now in Grails 1.3.7?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
request.forwardURI
。如果您指的是 GET 参数,那么应该使用上面的 URI 来解析?
我认为使用经典
重定向
不可能实现301重定向。您可以在这样的过滤器中执行此操作,这显然不是最干净的方法:Use
request.forwardURI
.If you have meant GET params, then it should be resolved using the above URI?
I think 301 redirects are not possible using classic
redirect
. You can do this in a filter like this, which is obviously not the cleanest way: