6

我想映射位于我的 web-app 目录中的静态文件 sitemap.xml 和 robots.txt。网址应如下所示:

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

如何设置 url 映射以使这些路由正常工作?

4

3 回答 3

10

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 ...
    }
}
于 2013-07-31T21:49:38.493 回答
8

我将此映射用于robots.txt

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

然后有一个grails-app/views/robots.gsp包含robots.txt. 通过这种方式,我可以<g:if env="...">轻松地为不同的环境提供不同的内容。

为了使它适用于“.xml”扩展,您需要更改内容协商配置。

grails.mime.file.extensions = false // disables the parsing of file extensions from URLs into the request format
于 2013-07-31T14:47:18.870 回答
0

如果您正在使用,对您的暂存环境设置一个 nofollow 也可能会有所帮助。不确定是否有将登台站点编入索引的用例....因此,如果您同意,您可以使用这些步骤来帮助阻止它。

如果您使用的是 Tomcat,请设置一个环境变量,例如NOFOLLOW=true --> 请参见此处,例如:TOMCAT_OPTS、环境变量和 System.getEnv()

接下来由@doelleri 提到设置 urlMappings

网址映射

//Robots.txt
"/robots.txt"(controller: 'robots', action:'robots')

然后使用您的 robotsController 检测您在暂存 tomcat 上设置的环境变量。

机器人控制器

def robots() {
    if (System.getenv('NOFOLLOW') == 'true') {
        def text = "User-agent: *\n" +
            "Disallow: /cgi-bin/ \n" +
            "Disallow: /tmp/ \n" +
            "Disallow: /junk/ \n" +
            "Disallow: /admin/ \n" +
            "Crawl-delay: 5 \n" +
            "Sitemap: https://www.example.com/sitemap.xml"

        render(text: text, contentType: "text/plain", encoding: "UTF-8")
    } else {
        render(status: 404, text: 'Failed to load robots.txt')
    }
}

机器人.gsp

<%-- Content rendered from controller -> so leave blank :) --%> 
于 2015-12-07T19:17:51.010 回答