{"id":1889,"date":"2010-10-14T21:01:54","date_gmt":"2010-10-14T15:31:54","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1889"},"modified":"2010-10-14T21:01:54","modified_gmt":"2010-10-14T15:31:54","slug":"get-absolute-url-of-site-grails-application-is-available-at","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/get-absolute-url-of-site-grails-application-is-available-at\/","title":{"rendered":"Get absolute URL of site Grails application is available at?"},"content":{"rendered":"<p>Many times, we need to get the absolute URL of the site\/application. One such case is when we are sending out links to the application in emails.<br \/>\n<br \/>\nThe default Config.groovy file provided by Grails has a property with the name <strong>grails.serverURL<\/strong>. I have typically used it to get absolute URL (environment specific) \u00a0of the site Grails application is available at.<br \/>\n<\/p>\n<p>This approach breaks in case the application is available as different sites i.e. the same application is \u00a0used to render multiple sites. \u00a0Here we need to get the absolute URL of the site that is serving the request (instead of application wide URL).<br \/>\n<\/p>\n<p>We could use inject a utility method to HttpServletRequest class (similar to isXhr method injected by Grails in the same class)<\/p>\n<p>[groovy]<br \/>\njavax.servlet.http.HttpServletRequest.metaClass.getSiteUrl = {<br \/>\n  return (delegate.scheme + &quot;:\/\/&quot; + delegate.serverName + &quot;:&quot; + delegate.serverPort + delegate.getContextPath())<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p>After we do this, we could simply say <strong>request.siteUrl<\/strong> to get the absolute URL of the site serving the request. You might want to get rid of serverPort in the above method if the server is running on standard HTTP ports (80\/443).<br \/>\n<\/p>\n<p>Hope you find it handy!<br \/>\n<\/p>\n<p>-Deepak<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many times, we need to get the absolute URL of the site\/application. One such case is when we are sending out links to the application in emails. The default Config.groovy file provided by Grails has a property with the name grails.serverURL. I have typically used it to get absolute URL (environment specific) \u00a0of the site [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":17},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1889"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1889"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1889\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}