2

現在、Python ボトルを使用して単純なスタンドアロン アプリケーションを作成しようとしています。

私のプロジェクト全体はpytest/、私が持っている と の下にdispatch.fcgiあり.htaccessます。

dispatch.fcgi:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import bottle
import os
from bottle import route, run, view

@route('<foo:path>')
@view('index')
def pytest(foo = ''):
    return dict(foo=foo)

APP_ROOT = os.path.abspath(os.path.dirname(__file__))
bottle.TEMPLATE_PATH.append(os.path.join(APP_ROOT, 'templates'))
app = bottle.default_app()

if __name__ == '__main__':
    from flup.server.fcgi import WSGIServer
    WSGIServer(app).run()

.htaccess:

DirectoryIndex dispatch.fcgi

次の URL から、対応する の値が得られますfoo

url.com/pytest/
> /pytest/

url.com/pytest/dispatch.fcgi
> /pytest/dispatch.fcgi

url.com/pytest/dispatch.fcgi/
> /

url.com/pytest/dispatch.fcgi/foo/bar
> /foo/bar

url.com/pytest/dispatch.fcgi/pytest/
> /pytest/

URL を統一するにはどうすればよいですか? .htaccessファイルまたは Python コード内で再ルーティングを処理する必要がありますか? 最もpythonicまたはベストプラクティスと見なされるものは何ですか?

Python 2.6.6、Bottle 0.11.6、Flup 1.0.2、および Apache 2.2.24 を実行しています。また、私は共有ホスティングを使用しており、mod_wsgi は問題外であることも指摘したいと思います (それが違いを生む場合)。

編集

これは私が期待するものです:

url.com/pytest/
> <redirect to url.com/pytest/dispatch.fcgi>

url.com/pytest/dispatch.fcgi
> <empty string>

url.com/pytest/dispatch.fcgi/
> /

url.com/pytest/dispatch.fcgi/foo/bar
> /foo/bar

url.com/pytest/dispatch.fcgi/pytest/
> /pytest/

この問題に取り組むためのより効率的な方法がある場合は、お知らせください。

4

2 に答える 2

1

末尾にスラッシュがあり、その後にパラメーターが続くことを期待しているため、ボトルは混乱しているようです。そのため、.htaccess ファイルを次のように変更しました。

DirectoryIndex dispatch.fcgi/

もう 1 つのオプションは、すべてのエラーをディスパッチ スクリプトに戻すことです。それはで行うことができますmod_rewrite

<IfModule mod_rewrite.c>
Options -MultiViews

# rewrite for current folder
RewriteEngine On
RewriteBase /pytest

# redirect to front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ dispatch.fcgi/ [R=301,QSA,L]
</IfModule>

またはFallbackResource:

FallbackResource /pytest/dispatch.fcgi/
于 2013-06-20T22:03:22.243 に答える