简体   繁体   English

Grails重定向包含尾随正斜杠

[英]Grails redirect to include trailing forward slash

I'm using Grails 2.3.6, and trying to redirect a URL to include a trailing forward slash, but to leave the rest of the URL as is. 我正在使用Grails 2.3.6,并尝试重定向URL以包含尾部正斜杠,但保留URL的其余部分。 The reason is because I have dynamic content which will have images and attachments. 原因是因为我有动态内容,会有图像和附件。 I would like those images and attachments to appear as child pages of the dynamic content, for example: 我希望这些图像和附件显示为动态内容的子页面,例如:

http://example.com/content

Will point to a dynamically generated piece of content, no issues. 将指向动态生成的内容,没有问题。 But, if I then want to have an attachment, I'll include an href link that points to attachments/file1.pdf . 但是,如果我想要一个附件,我将包含一个指向attachments/file1.pdf的href链接。 The problem is that with the above example it would produce http://example.com/attachments/file1.pdf (note that /content/ has been removed). 问题是,通过上面的示例,它将生成http://example.com/attachments/file1.pdf (请注意/content/已被删除)。

What I really want is for the path to look like: 我真正想要的是看起来像这样的道路:

http://example.com/content/attachments/file1.pdf

I figure the best workaround is to have the URLs for content be redirected to include a forward slash (if they don't already have one). 我认为最好的解决方法是将内容的URL重定向到包含正斜杠(如果它们还没有)。 Unfortunately I keep getting stuck with errors or redirect loops. 不幸的是,我一直遇到错误或重定向循环。

I've tried the suggestion here: Grails UrlMapping Redirect to keep DRY , but UrlMappings.groovy doesn't differentiate enough when I tell the same url to go to two different places (the only difference is the trailing slash) 我在这里尝试了这个建议: Grails UrlMapping Redirect保持DRY ,但当我告诉同一个url去两个不同的地方时,UrlMappings.groovy没有区别(唯一的区别是尾随斜线)

static mappings = {
    "/${content}/"(controller: "application", action: "index")
    "/${content}"(controller: "application", action: "redirectToAddSlash")
}

I also know that Grails 2.3 includes UrlMapping redirects, but the examples provided do not include embedded variables. 我也知道Grails 2.3包含UrlMapping重定向,但提供的示例不包含嵌入变量。

Ideally I would like to have something like this: 理想情况下,我希望有这样的东西:

static mappings = {
    "/${content}/"(controller: "application", action: "index") <=== Has trailing slash
    "/${content}"(redirect: "/${content}/")                    <=== Redirect to include trailing slash
}

Thanks in advance for any assistance! 在此先感谢您的帮助!

This solution solves the need of the problem, but not the exact resolution I was looking for in the question. 这个解决方案解决了问题的需要,但不是我在问题中寻找的确切解决方案。

Using the <base> element to provide the base href to use for all relative URLs appears to be the best workaround. 使用<base>元素提供用于所有相对URL的基本href似乎是最好的解决方法。

For the solution above I would use: 对于上面的解决方案,我会使用:

<base href="http://example.com/content">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM