简体   繁体   English

如何处理Python Bottle和Apache .htaccess中的URL重新路由?

[英]How do I deal with URL reroutes in Python Bottle and Apache .htaccess?

I am currently trying to create a simple standalone application using Python Bottle. 我目前正在尝试使用Python Bottle创建一个简单的独立应用程序。

My entire project is under pytest/ , where I have dispatch.fcgi and .htaccess . 我的整个项目都在pytest/ ,其中有dispatch.fcgi.htaccess

dispatch.fcgi : 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 : .htaccess

DirectoryIndex dispatch.fcgi

The following URLs give me the corresponding values of foo : 以下网址为我提供了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/

How can I make the URLs uniform? 如何使URL统一? Should I deal with the rerouting with the .htaccess file or within the Python code? 我应该使用.htaccess文件还是在Python代码中处理重新路由? What would be considered most pythonic, or best practices? 什么被认为是最Python性的或最佳实践?

I am running Python 2.6.6, Bottle 0.11.6, Flup 1.0.2, and Apache 2.2.24. 我正在运行Python 2.6.6,Bottle 0.11.6,Flup 1.0.2和Apache 2.2.24。 I would also like to point out that I'm using shared hosting, and mod_wsgi is out of the question (if that makes a difference). 我还想指出的是,我正在使用共享主机,而mod_wsgi是不可能的(如果有区别的话)。

EDIT 编辑

This is what I expect to see: 这是我期望看到的:

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/

If there is a more efficient way of tackling this problem, please let me know. 如果有解决此问题的更有效方法,请告诉我。

Couple of thoughts. 几个想法。 Hopefully some or all of these will help. 希望这些中的一些或全部会有所帮助。

1) You can do the redirect from '/' to '/pytest/dispatch.fcgi' like this: 1)您可以像这样从'/'重定向到'/pytest/dispatch.fcgi':

@route('/')
def home():
    bottle.redirect('/pytest/dispatch.fcgi')

2) Can you use ScriptAlias instead of DirectoryIndex? 2)您可以使用ScriptAlias代替DirectoryIndex吗? I see you're on a shared environment, so I'm not sure. 我看到您处于共享环境中,所以不确定。 My bottle/apache servers use ScriptAlias (or WSGIScriptAlias) and it works perfectly there; 我的bottle / apache服务器使用ScriptAlias(或WSGIScriptAlias),并且在那儿工作得很好。 and it'd make more clear the way your code interacts with apache. 并且可以使您的代码与apache交互的方式更加清晰。

3) If worse came to worst, could you hackishly detect the case where foo == '/pytest/dispatch.fcgi' and act accordingly? 3)如果情况变得更糟,您能不能暗中发现foo =='/pytest/dispatch.fcgi'并采取相应措施? (Eg, treat it as empty string.) (例如,将其视为空字符串。)

Hope this helps. 希望这可以帮助。 Please keep us posted! 请随时通知我们!

Bottle seems to be confused because it expects a trailing slash, followed by parameters. 瓶子似乎很困惑,因为它期望后面跟斜杠,后面跟参数。 For that reason I changed my .htaccess file to read like this: 因此,我将.htaccess文件更改为如下所示:

DirectoryIndex dispatch.fcgi/

Another option would be to have all errors fall back onto the dispatch script. 另一个选择是让所有错误都落在调度脚本上。 That can be done with mod_rewrite : 可以使用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>

or FallbackResource : FallbackResource

FallbackResource /pytest/dispatch.fcgi/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM