Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UrlMapping to static files in Grails

I want to map the static files sitemap.xml and robots.txt which a located in my web-app directory. The urls should be as follows:

http://www.mydomain.com/sitemap.xml 
http://www.mydomain.com/robots.txt

How do I have to set the url mapping to make these routes work?

like image 373
confile Avatar asked Jul 31 '13 14:07

confile


2 Answers

The simplest way is to tell grails to ignore them in UrlMappings.groovy:

class UrlMappings {
    static excludes = ['/robots.txt', '/sitemap.xml']

    static mappings = {
        // normal mappings here ...
    }
}
like image 146
codelark Avatar answered Nov 05 '22 07:11

codelark


I use this mapping for robots.txt:

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

And then have a grails-app/views/robots.gsp that contains the content for robots.txt. This way I can use <g:if env="..."> to easily have different content for different environments.

In order for this to work for a ".xml" extension, you need to change the Content Negotiation config.

grails.mime.file.extensions = false // disables the parsing of file extensions from URLs into the request format
like image 42
doelleri Avatar answered Nov 05 '22 09:11

doelleri