8

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.

knorv
  • 49,059
  • 74
  • 210
  • 294

2 Answers2

12

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)
cdeszaq
  • 30,869
  • 25
  • 117
  • 173
mbrevoort
  • 5,075
  • 6
  • 38
  • 48
  • Is it better to set all controller's redirects to status 301, as far as SEO is concerned, or should one leave them as 302 ? – Alexandre Bourlier Apr 17 '12 at 15:33
  • @Euoliix If a redirect is really permanent, then you should use 301. If Google sees 302 then supposedly it will check the URL again later. – Ken Liu May 08 '13 at 19:53
2

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.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173