Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

301 redirects in Grails

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.

like image 604
knorv Avatar asked Dec 23 '09 00:12

knorv


2 Answers

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)
like image 77
mbrevoort Avatar answered Sep 19 '22 02:09

mbrevoort


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.

like image 43
cdeszaq Avatar answered Sep 19 '22 02:09

cdeszaq