【问题标题】:Problems with Routing URLs using CGI and Bottle.py使用 CGI 和 Bottle.py 路由 URL 的问题
【发布时间】:2013-10-30 06:31:45
【问题描述】:

我一直很难在 CGI 环境中使用bottle.py 获得比简单索引/ 正确返回的任何东西。当我尝试返回 /hello 时,我收到 404 响应。但是,如果我请求 /index.py/hello

import bottle
from bottle import route

@route('/')
def index():
    return 'Index'

@route('/hello')
def hello():
    return 'Hello'

if __name__ == '__main__':
    from wsgiref.handlers import CGIHandler
    CGIHandler().run(bottle.default_app())

这是我的 .htaccess 文件

DirectoryIndex index.py
<ifmodule mod_rewrite.c="">
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.py/$1  [L]
</ifmodule>

当我使用 DH 时,我从这里复制了大部分代码,这似乎很相关:http://blog.coderonfire.com/2010/02/running-bottle-python-micro-framework.html

感谢您的帮助。

【问题讨论】:

    标签: python .htaccess cgi bottle


    【解决方案1】:

    问题是 &lt;ifmodule&gt; 块与您的 Apache 服务器无关,并且 mod_rewrite 的指令不起作用。从下面的.htaccess 开始,如果你有需要,根据你当前的 apache 版本添加块。

    DirectoryIndex index.py
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /index.py/$1  [L]
    

    【讨论】:

    • 谢谢,这行得通。我必须删除 /index.py/$1 上的 / 前面的 / 但它工作得很好。
    • @enrico,我不确定我是否喜欢你的编辑。我最好删除前导斜杠而不是删除 RewriteBase。
    • 设置 RewriteBase 在它只是一个斜线时似乎过分了,但如果它发生变化,肯定会让事情变得更容易。但是,我不只是删除前导斜杠的真正原因是编辑需要至少 6 个字符,因为愚蠢的“重要编辑”过滤器。如果这是您的偏好,您可以将其放回原处并删除斜线。
    • @enrico,另一个问题是编辑与原始问题无关=)我认为这不是您可以做的唯一增强:例如,我不确定在当前配置中查询字符串被传递到重定向页面(很长一段时间我没有碰过Apache配置)。
    猜你喜欢
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 2014-09-22
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    相关资源
    最近更新 更多