I would like to basically do a URL rewrite in UrlMappings.groovy, for example:
"/pub/cdn/$version/**"(uri: request.forwardURI.replaceFirst("/pub/cdn/[0-9]*", ""))
So that a request for:
/pub/cdn/1327516405188/css/login.css
for example, would get translated to:
/css/login.css
The idea is to take the original URI and strip out some of the path, and pass along the new URI.
However, using the above code, I get the error:
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'grailsUrlMappingsHolder': Cannot resolve
> reference to bean 'urlMappingsTargetSource' while setting bean
> property 'targetSource'; nested exception is
> ...
> groovy.lang.MissingPropertyException: No such property: forwardURI for
> class: java.lang.String
which seems to imply that the request object is a String. Is there a way to obtain the original URI in UrlMappings.groovy?
The "/old/path"(uri: "/new/path") method does work as long as the new path is hard coded, but I can't figure out how to access the original requested path as a variable inside the UrlMappings class.
P.S. I tried having UrlMappings call a controller:
"/pub/cdn/$version/**" {
controller = "image"
action = "cdnRedirect"
}
and in the Controller:
def cdnRedirect = {
def newUri = request.forwardURI?.toString().replaceFirst("/pub/cdn/[0-9]*", "")
return redirect(uri: newUri)
}
While the controller closure gets called correctly, and the URI gets translated correctly, the redirect does not seem to do anything at all.
Based on your example, it sounds to me like you are solving the wrong issue. The real issue here is seems to be that you are using relative links (e.g. css/login.css
) to static content. When the browser picks up a relative link, it uses the current url to determine the file's path.
However, if you use Grails' resource
tag, grails will generate a URL that is anchored to the root of the URL. For example, your css/login.css
link would become /myAppName/css/login.css
.
Usage is like so:
<link type="text/css" href="${resource(dir: 'css', file: 'login.css')}" />
This guarantees that, no matter the controller, action, or other ID information, the link to the CSS file will always be correct.
As for why your UrlMapping isn't working, it's because it isn't dynamically processed. It's processed during application startup, and so the request
object doesn't even exist at that time.
If you absolutely need a URL rewriting system, and using a controller to process the resources is too complex for this purpose, I recommend putting your servlet engine (i.e. Tomcat) behind Apache and just using mod_rewrite
. Properly designed, however, this really shouldn't be necessary.
Edit
Just for complete information, to get dynamic control of the mapping is to use the syntax specified in Dynamic Controller and Action Names. However, this is somewhat limited, and you still can't use it to perform a redirect.