Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. The problem is that with the above example it would produce http://example.com/attachments/file1.pdf (note that /content/ has been removed).

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). 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)

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.

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!

like image 502
mnd Avatar asked Mar 06 '14 20:03

mnd


1 Answers

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.

For the solution above I would use:

<base href="http://example.com/content">
like image 146
mnd Avatar answered Oct 21 '22 00:10

mnd