开发者

XML sitemap in Grails

I am trying to figure out the best way to generate an XML sitemap (as described here: http://www.sitemaps.org/) for a Grails application. I am not aware of any existing plugins that do this so I might build one. Ho开发者_如何学运维wever, I wanted to get the community's input first. Aside from supporting standard controllers/actions, I am thinking it would be nice to support content driven apps as well where the URL might be generated based on a title property for example.

How would you guys go about this? What would you consider and how would you implement it?

Thanks!


Sitemaps are pretty specific to each app so I'm not sure if there is enough common code to pull out to a plugin.

Here is how we generate our sitemap for http://www.shareyourlove.com. As you can see it's pretty minimal and DRY due to Groovy/Grails's nice XML syntax

class SitemapController{

        def sitemap = {
            render(contentType: 'text/xml', encoding: 'UTF-8') {
                mkp.yieldUnescaped '<?xml version="1.0" encoding="UTF-8"?>'
                urlset(xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
                        'xmlns:xsi': "http://www.w3.org/2001/XMLSchema-instance",
                        'xsi:schemaLocation': "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd") {
                    url {
                        loc(g.createLink(absolute: true, controller: 'home', action: 'view'))
                        changefreq('hourly')
                        priority(1.0)
                    }
                    //more static pages here
                    ...
                    //add some dynamic entries
                    SomeDomain.list().each {domain->
                    url {
                        loc(g.createLink(absolute: true, controller: 'some', action: 'view', id: domain.id))
                        changefreq('hourly')
                        priority(0.8)
                    }
                }
           }
    }

URL Mappings

class UrlMappings {
    static mappings = {

        "/sitemap"{
            controller = 'sitemap'
            action = 'sitemap'
        }
    }
}


I was doing a sitemap on Grails with a UrlMappings.groovy and does not need a controller for this practice. I putted the next code in the UrlMappings:

"/robots.txt" (view: "/robots")
"/sitemap.xml" (view: "/sitemap")

And I create my sitemap as gsp with xml encoding, example of sitemap.gsp:

<?xml version="1.0" encoding="UTF-8"?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

   <url>

      <loc>http://www.putYourSite.com.py/</loc>

      <lastmod>putAdate</lastmod>

      <changefreq>daily</changefreq>

      <priority>1.0</priority>

   </url>

</urlset>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜