【问题标题】:Block access to robots.txt on HTTP only, in app.yaml仅在 HTTP 上阻止对 app.yaml 中的 robots.txt 的访问
【发布时间】:2017-10-06 06:59:39
【问题描述】:

我需要在robots.txt上引发404 Not Found,同时从HTTP访问,在HTTPSrobots.txt应该正常返回。

我无法找到仅在 app.yaml 配置中限制访问 https 的方法,因此我决定为此编写一个处理程序,但出现以下错误:

google.appengine.api.yaml_errors.EventError: Unexpected attribute "script" for mapping type static_files.    

app.yaml 我有:

- url: /robots.txt
  script: main.application
  static_files: static/\1
  upload: static/robots.txt

处理这种情况的最佳方法是什么?

【问题讨论】:

    标签: google-app-engine yaml


    【解决方案1】:

    简单的处理程序会做:

    app.yaml

    - url: /robots_file
      static_files: static/robots.txt
      upload: static/robots.txt
    

    views.py:

    from google.appengine.api.urlfetch import fetch
    
    class RobotsTxtHandler(webapp2.RequestHandler):
    
        def get(self):
            if self.request.url.startswith('https'):
                robots = fetch('{}/robots_file'.format(SITE_URL))
                return self.response.write(robots.content)
        raise errors.Http404
    

    urls.py:

    urls = [
       ('/robots.txt', views.RobotsTxtHandler),
    ]
    

    【讨论】:

      【解决方案2】:

      在 app.yaml 中为您的处理程序设置 secure: always

      文档:https://cloud.google.com/appengine/docs/standard/python/config/appref

      secure: always - 与此处理程序匹配但不使用 HTTPS 的 URL 请求会自动重定向到具有相同路径的 HTTPS URL。为重定向保留查询参数。

      在你的情况下:

      - url: /robots.txt
        static_files: static/robots.txt
        upload: static/robots.txt
        secure: always
      

      【讨论】:

      • 在我的情况下它没有帮助,因为爬虫忽略了重定向,他们会将 https/robots.txt 视为 http/robots.txt。
      • 如果您只需要使您的站点为 https,您应该为所有处理程序指定安全:始终并在 HTML 中设置规范元标记?您可以使用DebtsTracker.io 查看我是如何做到的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      相关资源
      最近更新 更多