Skip to main content
 首页 » 编程设计

python之lighttpd、mod_rewrite 和 web.py 意外行为

2025年05月04日37telwanggs

我已经将 lighttpd 配置为与 web.py 一起工作。现在我想通过我的 web.py 脚本处理 mysite.com/scripts/* 形式的所有请求。这是 lighttpd.conf 的相关部分的样子:

fastcgi.server = ("/code.py" => 
(("socket" => "/var/tmp/lighttpd/fastcgi.socket", 
  "bin-path" => "/var/www/code.py", 
  "max-procs" => 1, 
  "check-local" => "disable", 
)) 
) 
 
url.rewrite-once = ( 
  "^/scripts/(.*)$" => "/code.py/$1", 
) 

我设置了一个简单的 code.py 来打印 URL 中出现的内容。这是代码:

urls = ( 
    '(.*)', 'hello') 
 
app = web.application(urls, globals(),True) 
 
class hello: 
    def GET(self, x): 
        return "I have received: " + web.websafe(x) 

当我输入 mysite.com/code.py/test 时,我看到 “我已收到:/test”,这是正确的,但是当我输入 < strong>mysite.com/scripts/test,我看到“我收到了:/scripts/test”。

我期待重写规则匹配/scripts/之后的内容并将 URL 重写为/code.py/test,为什么它也通过了/scripts 部分?

请您参考如下方法:

"/scripts/test" 和"/test" 被(.*) 整体匹配。如果你只想匹配两个 URL 的 "test" 部分,你可以这样写。

urls = ( 
  r'(.*)/(.*)', 'hello', 
) 
 
app = web.application(urls, globals(), True) 
 
class hello(object): 
  def GET(self, x, y): 
    return 'i have received ' + web.net.websafe(y)