I'm currently doing my Grails 301 URL-redirects using the following quite cumbersome "servlet style" method:
def action = {
...
if (shouldRedirect) {
response.status = 301
response.setHeader("Location", "http://url/to/redirect/to.html")
render("")
return false
}
...
}
Is there any cleaner and more compact Groovy/Grails'y way to perform a 301 redirect?
Please note that I'm talking about 301 redirect, not the standard 302 redirects which can be achieved using the standard Grails redirect(...)
mechanism.
Yes, it's now possible to use redirect
and specify the permanent
parameter as true
as described here. For example:
redirect(url: "http://url/to/redirect/to.html", permanent: true)
The redirect
mechanism in Grails currently supports a permanent
parameter:
permanent
(optional) - If true the redirect will be issued with a 301 HTTP status code (permanently moved), otherwise a 302 HTTP status code will be issued
This should adequately solve your problem, and in a very Grails-y way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With