2017-05-08 59 views
0

我需要在robots.txt上提升404 Not Found,而從HTTP訪問時,在HTTPSrobots.txt應該正常返回。阻止對HTTP上的robots.txt的訪問,在app.yaml

我找不到僅限制在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 

處理這種情況的最佳方法是什麼?

回答

0

簡單的處理就可以了:

的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), 
] 
0

設置secure: always的應用程序。 yaml爲你處理。

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

secure: always - Requests for a URL that match this handler that do not use HTTPS are automatically redirected to the HTTPS URL with the same path. Query parameters are preserved for the redirect.

你的情況:

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

在我的情況下,它不會幫助,因爲爬蟲被忽略重定向,他們會處理HTTPS/robots.txt的作爲HTTP/robots.txt的。 – Pythonist

+0

如果您需要讓您的網站只有https可能應該指定安全:始終爲所有處理程序和在HTML中設置規範元標記?你可以用http://DebtsTracker.io/查看我是如何做到的 –