我想映射静态文件sitemap.xml和robots.txt,它们位于我的web应用程序目录中。网址应如下:
http://www.mydomain.com/sitemap.xml
http://www.mydomain.com/robots.txt如何设置url映射才能使这些路由正常工作?
发布于 2013-07-31 14:47:18
我将此映射用于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发布于 2013-07-31 21:49:38
最简单的方法是在UrlMappings.groovy中告诉grails忽略它们
class UrlMappings {
static excludes = ['/robots.txt', '/sitemap.xml']
static mappings = {
// normal mappings here ...
}
}发布于 2015-12-07 19:17:51
如果您正在使用暂存环境,则对暂存环境设置nofollow也可能会有所帮助。不确定是否有一个用例有一个中转站点索引.因此,如果您同意,您可能能够使用这些步骤来帮助阻止这一点。
如果您正在使用Tomcat,请设置一个环境变量,例如NOFOLLOW=true ->参见这里的示例:OPTS, environment variable and System.getEnv()
接下来,正如@doelleri所提到的,设置urlMappings
UrlMappings
//Robots.txt
"/robots.txt"(controller: 'robots', action:'robots')然后使用您的robotsController来检测您在暂存tomcat上设置的环境变量。
RobotsController
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')
}
}robots.gsp
<%-- Content rendered from controller -> so leave blank :) --%> https://stackoverflow.com/questions/17973383
复制相似问题