使用Flask,如何将robots.txt和sitemap.xml作为静态文件提供?[重复]

发布于 2021-01-29 15:06:47

这个问题已经在这里有了答案

Flask中的静态文件-
robot.txt,sitemap.xml(mod_wsgi)
(10个答案)

5年前关闭。

我已经在安静的地方阅读过一些内容,这些内容应该将静态文件提供给服务器,例如,关于SO问题的几个答案。但是我使用的是OpenShift
PaaS,无法弄清楚在那里如何修改.htaccess文件。

我碰到了这段代码,它通过模板为站点地图提供服务。我在应用程序中同时针对站点地图和robots.txt做到了这一点,就像这样-

@app.route("/sitemap.xml")
def sitemap_xml():
    response= make_response(render_template("sitemap.xml"))
    response.headers['Content-Type'] = 'application/xml'
    return response

@app.route("/robots.txt")
def robots_txt():
    return render_template("robots.txt")

这有什么危害吗,或者我的方法还可以吗?

关注者
0
被浏览
94
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    robots.txtsitemap.xml到您的应用程序的static目录,并定义了这样的观点:

    from flask import Flask, request, send_from_directory
    
    @app.route('/robots.txt')
    @app.route('/sitemap.xml')
    def static_from_root():
        return send_from_directory(app.static_folder, request.path[1:])
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看