我想映射位于我的web-app目录中的静态文件sitemap.xml和robots.txt。网址应该如下:UrlMapping到Grails中的静态文件
http://www.mydomain.com/sitemap.xml
http://www.mydomain.com/robots.txt
我该如何设置url映射使这些路由起作用?
我想映射位于我的web-app目录中的静态文件sitemap.xml和robots.txt。网址应该如下:UrlMapping到Grails中的静态文件
http://www.mydomain.com/sitemap.xml
http://www.mydomain.com/robots.txt
我该如何设置url映射使这些路由起作用?
我用这个映射robots.txt
:
"/robots.txt" (view: "/robots")
,然后有一个grails-app/views/robots.gsp
,其中包含robots.txt
内容。这样我可以使用<g:if env="...">
轻松地为不同的环境提供不同的内容。
为了使其适用于“.xml”扩展名,您需要更改Content Negotiation配置。
grails.mime.file.extensions = false // disables the parsing of file extensions from URLs into the request format
最简单的方法就是告诉Grails的忽略它们在UrlMappings.groovy
:
class UrlMappings {
static excludes = ['/robots.txt', '/sitemap.xml']
static mappings = {
// normal mappings here ...
}
}
该答案更准确地表示OP请求的内容,虽然所选答案提供了一种工作替代方法。 – mnd
它也可能会有所帮助的nofollow的设置为你的暂存环境,如果您正在使用一个。不知道这些是否是一个使用登录站点索引的用例....所以如果您同意,您可以使用这些步骤来帮助阻止该问题。
如果正在使用Tomcat,设置一个环境变量如NOFOLLOW =真 - >在这里看到例如:TOMCAT_OPTS, environment variable and System.getEnv()
接着通过@doelleri提到设置的urlMappings
UrlMappings
//Robots.txt
"/robots.txt"(controller: 'robots', action:'robots')
然后使用你的robotsController来检测envir在您的暂存tomcat上设置的启动变量。
RobotsController
def robots() {
if (System.getenv('NOFOLLOW') == 'true') {
render(view: 'robots')
} else {
render(status: 404, text: 'Failed to load robots.txt')
}
}
robots.gsp
<%@ page contentType="text/plain;charset=UTF-8" %>User-agent: *
Disallow:/
感谢。它适用于机器人,但不适用于sitemap.xml,你会怎么做? – confile
它为什么不起作用? – doelleri
“/sitemap.xml”(view:“/ sitemap”)不起作用。 – confile