在 Grails 中从一个 Web 域重定向到另一个 Web 域的最佳实践?

发布于 2024-11-29 13:08:37 字数 971 浏览 0 评论 0原文

我目前正在 Grails 中开发一个过滤器,它允许我将 foo.org 上的所有传入请求重定向到 foo.com 上的同一子页面。 到目前为止,我一直在执行以下操作:

 if(!(""+request.requestURL).toLowerCase().startsWith(
             grailsApplication.config.grails.serverURL ))
    {redirect(url:"${grailsApplication.config.grails.serverURL}${request.requestURI}",params:params) }

不幸的是,我在这种方法中遇到了几个问题:

  1. request.requestURI 值的行为似乎与预期不同:而不是给我正常的“/[controller]/[action]”模式我期望它返回类似:“/grails/[controller]/[action].dispatch”。 - 是否有其他方法来获取“正常”URI? (请原谅,如果这是微不足道的,但无法在文档中找到它,也无法尝试请求对象上可用的各种方法)
  2. 参数没有在上面的重定向中传递。这可能是由于我在重定向中使用“url”参数,根据文档,该参数应该用于重定向到绝对路径(这又导致它忽略 params 部分?)。但是,由于在重定向到另一个域时我将无法使用正常的重定向(控制器:...,操作:...)方法,因此我可以使用什么方法将参数正确传递到 foo 上的子页面.com?正在考虑一种解决方案,我将通过 params.each{} 闭包手动添加参数,但是是否有更优雅的解决方案?
  3. 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:

  1. 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)
  2. 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?
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只涨不跌 2024-12-06 13:08:37
  1. 使用request.forwardURI

  2. 如果您指的是 GET 参数,那么应该使用上面的 URI 来解析?

  3. 我认为使用经典重定向不可能实现301重定向。您可以在这样的过滤器中执行此操作,这显然不是最干净的方法:

    def 过滤器 = {
        所有(控制器:'*',操作:'*'){
            之前 = {
                if (request.serverName == "foo.org") {
                    响应.setStatus(301);
                    response.setHeader("位置", "http://foo.com" + request.forwardURI)
                    响应.flushBuffer()
                    返回假; // 返回 false,否则请求由控制器处理
                }
            }
        }
    }
    
  1. Use request.forwardURI.

  2. If you have meant GET params, then it should be resolved using the above URI?

  3. 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:

    def filters = {
        all(controller:'*', action:'*') {
            before = {
                if (request.serverName == "foo.org") {
                    response.setStatus(301);
                    response.setHeader("Location", "http://foo.com" + request.forwardURI)
                    response.flushBuffer()
                    return false; // return false, otherwise request is handled from controller
                }
            }
        }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文