I am working with Grails 2.1.1 and would like to add some customized URLs that map to controller actions.
I can do this, but the original mapping still works.
For example, I created an add-property-to-directory UrlMappings in my UrlMappings as follows:
class UrlMappings { static mappings = { "/add-property-to-directory"(controller: "property", action: "create") "/$controller/$action?/$id?"{ constraints { // apply constraints here } } "/"(view:"/index") "500"(view:'/error') } }
Now I can press /mysite/add-property-to-directory and it will execute PropertyController.create , as expected.
However, I can still press /mysite/property/create and it will execute the same PropertyController.create method.
In the spirit of DRY, I would like to do 301 Redirect from /mysite/property/create to /mysite/add-property-to-directory .
I could not find a way to do this in UrlMappings.groovy . Does anyone know how I can do this in the Grail?
Many thanks!
UPDATE
Here is the solution I implemented based on Tom's answer:
UrlMappings.groovy
class UrlMappings { static mappings = { "/add-property-to-directory"(controller: "property", action: "create") "/property/create" { controller = "redirect" destination = "/add-property-to-directory" } "/$controller/$action?/$id?"{ constraints { // apply constraints here } } "/"(view:"/index") "500"(view:'/error') } }
RedirectController.groovy
class RedirectController { def index() { redirect(url: params.destination, permanent: true) } }
Philip tenn
source share