使用CGI和Bottle.py路由URL时遇到的问题

6 投票
1 回答
3277 浏览
提问于 2025-04-15 21:45

我在使用bottle.py这个框架时,遇到了一些麻烦,特别是在CGI环境下,想要返回的内容除了简单的首页以外都不太正常。当我尝试访问/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

谢谢大家的帮助。

1 个回答

4

问题在于,<ifmodule> 这个部分和你的Apache服务器没有关系,所以里面的指令对mod_rewrite没有作用。你可以先从下面这个 .htaccess 文件开始,如果有需要的话,再根据你当前的Apache版本添加那个部分。

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

撰写回答