【问题标题】:Where to place a robots.txt file in a Python 2.7/Bottle application on OpenShift?在 OpenShift 上的 Python 2.7/Bottle 应用程序中放置 robots.txt 文件的位置?
【发布时间】:2014-07-19 17:59:57
【问题描述】:

环境

  • Python 2.7
  • OpenShift

应用结构:

.git
.openshift
data
libs
wsgi
 - static
 - views
 - application
 - my_bottle_app.py
README.md
setup.py
setup.pyc
setup.pyo

期望的行为

我想为该位置的文件创建robots.txt 规则:

wsgi/static/file_1.txt
wsgi/static/file_2.txt

例如:

User-agent: *
Disallow: /file_1.txt
Disallow: /file_2.txt

问题

robots.txt 文件是否应该放在其中一个

  • wsgi
  • wsgi/static
  • 还是应用程序结构的“根”?

编辑:

为了澄清,该应用程序是一个Bottle 应用程序,因此有许多路由服务于不同的内容。

此外,所有页面都通过 https 提供自定义功能:

def redirect_http_to_https(callback):
    '''Bottle plugin that redirects all http requests to https'''

    def wrapper(*args, **kwargs):
    scheme = request.urlparts[0]
    if scheme == 'http':
        # request is http; redirect to https
        redirect(request.url.replace('http', 'https', 1))
    else:
        # request is already https; okay to proceed
        return callback(*args, **kwargs)
    return wrapper

install(redirect_http_to_https)

所以我试图了解robots.txt 应该放在哪里,以便正确提供服务。

【问题讨论】:

    标签: python-2.7 openshift robots.txt bottle


    【解决方案1】:

    将 robots.txt 文件放在后端的哪个位置并不重要。
    仅在可从 Web 访问 robots.txt 的位置才重要。

    对于每个主机,文件必须位于/robots.txt。因此它必须始终位于主机的根目录中,而不是位于子文件夹中。

    示例:

    当机器人想要抓取 http://example.com/wsgi/static/file_1.txt 时,它应该在 http://example.com/robots.txt 上查找 robots.txt。

    如果是https://example.com/wsgi/static/file_1.txt(https而不是http),则位置必须是https://example.com/robots.txt
    如果是http://www.example.com/wsgi/static/file_1.txt(带子域),位置必须是http://www.example.com/robots.txt

    【讨论】:

      【解决方案2】:

      解决方案

      这是根据用户 unor 的回答似乎有效的具体解决方案。

      在 Python 应用中添加 Bottle 路由:

      @route('/robots.txt')
      def serve_robots():
          return static_file('robots.txt', root='app-root/repo/wsgi/static/')
      

      然后将robots.txt 添加到wsgi/static/

      robots.txt 文件随后可在以下位置访问。

      https://app-username.rhcloud.com/robots.tx
      

      【讨论】:

        猜你喜欢
        • 2015-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-20
        • 1970-01-01
        • 2014-01-10
        • 2011-11-30
        • 1970-01-01
        相关资源
        最近更新 更多