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

lighttpd服务器下CMS因mod_rewrite配置及.htaccess不兼容导致404错误求助

lighttpd服务器下CMS因mod_rewrite配置及.htaccess不兼容导致404错误求助

嘿,别慌!lighttpd确实不像Apache那样支持.htaccess文件,得把你原来的Apache规则转换成lighttpd的配置才行。我帮你一步步整理好对应的配置,解决404问题,同时把压缩、缓存这些功能也搞定~

第一步:先整理你的lighttpd模块配置

你当前的server.modules里有重复的注释和启用项,先把它清理干净,确保需要的模块都正确启用:

server.modules = (
    "mod_magnet",
    "mod_indexfile",
    "mod_access",
    "mod_alias",
    "mod_rewrite",
    "mod_redirect",
    "mod_compress",
    "mod_simple_vhost",
    "mod_openssl",
    "mod_expires"  # 新增这个模块,用于缓存过期配置
)

注意:如果你的lighttpd没有mod_expires,可能需要先安装对应的包(比如Debian/Ubuntu下是lighttpd-mod-expire),或者编译时包含这个模块

第二步:配置URL重写规则(解决404的核心)

把你Apache .htaccess里的重写规则转换成lighttpd的写法,这部分是解决页面404的关键:

# 对应Apache的RewriteEngine On
url.rewrite-if-not-file = (
    "^/(.*)$" => "/index.php?path=$1"
)

这个规则的作用和你原来的.htaccess完全一致:如果请求的文件或目录不存在,就把URL重写到index.php?path=xxx,同时保留查询参数(Apache里QSA的效果lighttpd默认支持)。

第三步:配置内容压缩(对应mod_deflate)

lighttpd用mod_compress实现和Apache mod_deflate一样的压缩功能,添加以下配置:

compress.cache-dir = "/var/cache/lighttpd/compress"
compress.filetype = (
    "text/html", "text/css", "text/javascript",
    "text/xml", "text/plain", "image/x-icon",
    "image/svg+xml", "application/rss+xml",
    "application/javascript", "application/x-javascript",
    "application/xml", "application/xhtml+xml",
    "application/x-font", "application/x-font-truetype",
    "application/x-font-ttf", "application/x-font-otf",
    "application/x-font-opentype", "application/vnd.ms-fontobject",
    "font/ttf", "font/otf", "font/opentype"
)
# 浏览器兼容规则
compress.useragent = (
    "!^Mozilla/4",
    "!^Mozilla/4\.0[678]",
    "!^MSIE"
)
compress.vary = "User-Agent"

记得创建压缩缓存目录并设置权限:mkdir -p /var/cache/lighttpd/compress && chown lighttpd:lighttpd /var/cache/lighttpd/compress

第四步:配置缓存过期(对应mod_expires)

启用mod_expires后,添加以下配置来实现和你原来.htaccess一样的缓存策略:

expires.url = (
    "*.jpg" => "access 1 year",
    "*.jpeg" => "access 1 year",
    "*.gif" => "access 1 year",
    "*.png" => "access 1 year",
    "*.css" => "access 1 month",
    "*.pdf" => "access 1 month",
    "*.js" => "access 1 month",
    "*.swf" => "access 1 month",
    "*.ico" => "access 1 year",
    "" => "access 2 days"  # 默认规则
)

第五步:关于mod_security的配置

如果你的lighttpd安装了mod_security,对应的规则是:

secfilter-scan-post = "Off"

如果没装这个模块,可以直接忽略这部分配置。

最后一步:重启lighttpd服务

修改完配置后,重启lighttpd让配置生效:

# 根据你的系统选择命令
systemctl restart lighttpd  # systemd系统(Ubuntu 16.04+/CentOS 7+)
# 或者
service lighttpd restart    # 旧版SysVinit系统

这样配置后,你的CMS应该就能正常工作,不会再出现404错误了,同时原来.htaccess里的压缩、缓存功能也都能实现~

备注:内容来源于stack exchange,提问作者Prajwal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.21 09:23:06