Search code examples
urlgrailsgsp

Get Grails to generate relative path in its relative URL


I have a web application that can be accessed either directly as http://host.foo.loc:8080/foo/ or via a secure reverse proxy as http://www.company.com/apps/foo/

By default, Grails will generate relative URLs with relative paths, for example:

<g:form action="bar">

will produce:

<form action="/foo/bar" method="post" >

This will be OK locally but the reverse proxy will not accept http://www.company.com/foo/bar (it's missing /apps/)

If I do:

<g:form action="bar" base=".">

it's fine. But I don't really want to specify it on each and every tag that generates a link.

The best way to deal with this would be to get Grails to generate relative paths in its relative URLs. Alternatively, I could live with setting a global "baseUrl" to "." but I don't know how to do that either.

Any idea ?

[edit] In fact, setting the "base" to "." doesn't work. The first page "/foo/controller/action" will generate the link as "./controller/nextaction" which the browser will translate as "/foo/controller/controller/action" => 404. I guess this is why they're using absolute paths: they're not paths.


Solution

  • I've never been able to make this kind of scenario work, so I always keep the proxied and unproxied context paths the same, i.e. I would put the app at http://host.foo.loc:8080/apps/foo. You can have a multi-level context path like this in Tomcat by naming the WAR file apps#foo.war.