Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails SEO friendly URLs

The standard way of creating URLs in grails is:

<a href="${createLink(controller:'news', action: 'show', params: [id: news.id])}">${news.title}</a>

which generates the url: /news/show/102

I want more SEO friendly URLs like:

/news/102/this-is-the-hottest-news-today

What is the cleanest way to do this in Grails? I could use grails URLMapping to map /news/show/102 to /news/102, but how I do create the complete URL like above?

like image 572
Langali Avatar asked Mar 01 '10 17:03

Langali


1 Answers

You could turn the headline into a parameter like this:

name story: "/news/$id/$headline" {
    controller = "news"
    action = "show"
}

That way you could create your urls with the headline in them and the mapping would still work. You of course don't actually have to use the headline parameter that will appear in your controller. The example above uses a named URL mapping so you can then say:

${createLink(mapping: "story", params: [id: 102, headline: 'this-is-the-hottest-news-today'])}

You may also be interested in this plugin for creating canonical urls - http://www.grails.org/plugin/canonical

like image 104
Dave Bower Avatar answered Oct 02 '22 19:10

Dave Bower