Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Flask, how can I serve robots.txt and sitemap.xml as static files? [duplicate]

I've read on quiet a few places that serving static files should be left to the server, for example in a couple of the answers on this SO question. But I use the OpenShift PaaS, and can't figure out how to modify the .htaccess file there.

I came across this piece of code that serves the sitemap from a template. I did that on my app for both the sitemap, and robots.txt, like so -

@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") 

Is there any harm in this, or is my approach okay?

like image 738
elssar Avatar asked Dec 27 '12 02:12

elssar


People also ask

Should Sitemap XML be in robots txt?

Robots. txt files should also include the location of another very important file: the XML Sitemap. This provides details of every page on your website that you want search engines to discover.

What is the difference between robots txt and Sitemap XML?

The sitemap. xml provides search engines with a direct path to each page of your site, which offer quick indexing for all the pages whereas robots. txt is used to tell search engines what pages to crawl and what not to crawl.

Why do we create sitemap XML and robots txt file?

Robots. txt and sitemap. xml are essential files that can help search engines better understand your particular website and index it correctly.


1 Answers

Put robots.txt and sitemap.xml into your app's static directory and define this view:

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:]) 
like image 109
Audrius Kažukauskas Avatar answered Sep 19 '22 18:09

Audrius Kažukauskas