Michael
Michael

Reputation: 33317

How do I set default Urls in Grails UrlMappings?

I want to include a default url mapping for Grails. This is, if an url is requested which has no rule in the url mapping should be redirected to home/index.

My url mapping currently is:

class UrlMappings {

    static mappings = {
       "/$controller/$action?/$id?(.$format)?" { 
           constraints { 
           // apply constraints here
           } 
        }

       "/"(controller:"home", action:"index")

       "/admin"(controller:"login",action:"authadmin")
       "/robots.txt"(controller:"home",action:"robots")
       "/sitemap.xml"(controller:"home",action:"sitemap")

       "404"(controller:"home",action:"notfound")
       "500"(view:'/error')

       "/**"(controller:"home", action:"index")    
    }
}

I use the last rule starting with /** to map all incoming requests which do not have a certain rule in the url mapping. The problem is that when I put the following in my GSP file

<g:link controller="home" action="index"><div class="MainLogo"></div></g:link>

Then the url looks like this:

http://www.example.com/**

Is there a way to correct this? How do I create a default mapping for urls where no rule in the url mapping was defined?

Upvotes: 2

Views: 1408

Answers (1)

Graeme Rocher
Graeme Rocher

Reputation: 7985

I can't say I really advise this approach, since really it is better to send a 404 back for pages that don't exist, but anyway I suppose the best way to go about this is a double wild card path. See:

http://grails.org/doc/latest/guide/theWebLayer.html#mappingWildcards

For example:

   "/$path**"(controller:"home", action:"index")

Upvotes: 3

Related Questions