Search code examples
python-3.xflaskherokugoogle-search-console

How to use html file upload method for google site verification for a flask app


The pretty straight forward way to do this is to upload the google-provided.html file to the root folder of your app on the server. But how to do it for a flask application?

For example, I have a flask app running on heroku and I want to do the site verification for my app using html file upload method(though alternative methods are available). I tried uploading the google-provided.html in the templates folder. Verification failed!

I have searched the internet but found no relevant answers.


Solution

  • Flask uses directory structure that contains /templates folder where our html files should be present.

    To access any .html file, there needs to be a route and a view defined. So, in order to make your site verification process success, in addition to placing the google-provided.html file in templates folder, you need to define a route and a view so that the google bot which tries to access the google-provided.html file can find it.

    The route should be /google-provided.html and view should render the template google-provided.html because the google bot looks at app.yourdomain.com/google-provided.html for the google-provided.html file in your app.

    Simply put, it should look something like this:

    @app.route("/google-provided.html")
    def google_site_verf():
        return render_template("google-provided.html")
    

    I encountered this issue today and found the solution myself. Hence thought of sharing this here so that it could help someone.

    Feel free to drop suggestions/improvements. Cheers!