与web.py匹配的URL

问题描述:

我正在用apache和web.py编写一个简单的“Hello world”。与web.py匹配的URL

http://sandbox-dev.com/webapp 

我的直觉(显然是错误的)是,下面的代码会匹配任何这些地址:当我去

http://sandbox-dev.com/webapp/ 

,但是当我去不是该应用程序的工作原理。

import sys, os 
abspath = os.path.dirname(__file__) 
sys.path.append(abspath) 
os.chdir(abspath) 

import web 

urls = (
     '/.*', 'hello', 
    ) 

class hello: 
     def GET(self): 
      return "Hello, web.py world." 

application = web.application(urls, globals()).wsgifunc() 

我需要改变以匹配这两者吗?

当URL是“http://sandbox-dev.com/webapp”时,web.py将其视为“”,而不是“/”。因此,将url格式更改为“。*”将起作用。

但是,可能你应该修复你的Apache配置,而不是在webapp。添加规则以将/ webapp重定向到/ webapp /。

如果你想有头版由类你好进行处理,所有你需要的是这样的:

urls = (
    '/', 'hello', 
) 

还是我误解你的意图?

@Anand Chitipothu是对的。

http://sandbox-dev.com/webapp/ matches '/' 

http://sandbox-dev.com/webapp matches '' #empty string 

,所以如果你要修复它在webpy,你可以这样写:

urls = (
    '.*', 'hello' #match all path including empty string 
    ) 

或添加重定向类

urls = (
    '/.*', 'hello', 
    '', 'Redirect' 
    ) 
class Redirect(object): 
    def GET(self): 
     raise web.seeother('/') # this will make the browser jump to url: http://sandbox-dev.com/webapp/