You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu下Lighttpd条件重定向配置求助:构建PHP路由

Lighttpd 条件重定向与PHP路由配置方案

1. 启用必要模块

确保lighttpd.conf中已启用以下模块(未添加则补上):

server.modules += ( "mod_rewrite", "mod_redirect" )

若需处理PHP请求,需同时启用mod_fastcgi或mod_cgi(根据你的PHP部署方式选择)。

2. 添加条件路由规则

在lighttpd.conf的服务器配置块(server { ... })内插入以下规则:

# 放行/clients/和/admin/开头的请求
$HTTP["url"] !~ "^/(clients|admin)/" {
    # 非真实文件/目录的请求,转发到index.php并保留查询参数
    url.rewrite-if-not-file = (
        "^/(.*)$" => "/index.php?$QUERY_STRING"
    )
}

3. 规则说明

  • $HTTP["url"] !~ "^/(clients|admin)/":匹配所有不以/clients/或/admin/开头的请求
  • url.rewrite-if-not-file:仅当请求路径对应的文件/目录不存在时,触发内部转发
  • "^/(.*)$" => "/index.php?$QUERY_STRING":将请求转发到根目录index.php,同时完整保留原请求的查询参数(比如/abc123?foo=bar会转发为/index.php?foo=bar)

4. 生效配置

保存lighttpd.conf后,重启Lighttpd服务:

sudo systemctl restart lighttpd

测试验证

  • 访问https://www.example.com/clients/xyz或https://www.example.com/admin/xyz,应正常加载对应路径内容
  • 访问https://www.example.com/abc123?test=123,应内部转发到index.php,且PHP代码中$_GET['test']能获取到123

内容的提问来源于stack exchange,提问作者Jason Johnson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 12:27:11