Grails SEO friendly URLs
Asked Answered
S

1

6

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?

Stickweed answered 1/3, 2010 at 17:47 Comment(0)
E
15

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

Edmundoedmunds answered 2/3, 2010 at 9:31 Comment(1)
Dave thanks! This method is very handy for multi language sites, where you can target particular mapper depending on current locale. In my example I have story-en and story-hr mappings and in my g:link I use mapping: "story-${locale}" which selects proper mapper for selected locale.Winnow

© 2022 - 2024 — McMap. All rights reserved.