小编典典

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

flask

我使用的是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")

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


阅读 838

收藏
2020-04-06

共1个答案

小编典典

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:])
2020-04-06