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
相关产品推荐
相关产品推荐

