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

CodeIgniter4部署后CSS文件404问题求助(Ubuntu22.04环境)

问题排查:CodeIgniter4部署Ubuntu 22.04后部分CSS文件404

原CentOS7+Plesk环境运行的CodeIgniter4应用,意外后在Ubuntu22.04服务器重新部署:

  • 创建子域名并上传代码
  • 修改http.conf将文档根指向public目录
  • 配置app/Config/App.php的baseURL为https://patron.berkyazilim.com/

首页可正常加载,但部分CSS文件(如style.default.css)返回404,同目录下的custom.css可正常访问。

public/css目录文件列表:

custom.css              style.default.css          style.green.min.css      style.pink.min.css.map  style.sea.css          style.violet.min.css
style.blue.css          style.default.min.css      style.green.min.css.map  style.red.css           style.sea.min.css      style.violet.min.css.map
style.blue.min.css      style.default.min.css.map  style.pink.css           style.red.min.css       style.sea.min.css.map
style.blue.min.css.map  style.green.css            style.pink.min.css       style.red.min.css.map   style.violet.css

当前.htaccess配置:

# Disable directory browsing
Options All -Indexes

# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymlinks must be enabled for this to work.
<IfModule mod_rewrite.c>
        Options +FollowSymlinks
        RewriteEngine On

        # If you installed CodeIgniter in a subfolder, you will need to
        # change the following line to match the subfolder you need.
        # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
        # RewriteBase /

        # Redirect Trailing Slashes...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} (.+)/$
        #RewriteCond $1 !^(index\.php|assets|assets/css|assets/js|assets/etc|robots\.txt)
        RewriteRule ^ %1 [L,R=301]
        #RewriteRule ^(.*)$ /gestion/index.php/$1 [L]

        # Rewrite "www.example.com -> example.com"
        RewriteCond %{HTTPS} !=on
        RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
        RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

        # Checks to see if the user is attempting to access a valid file,
        # such as an image or css document, if this isn't true it sends the
        # request to the front controller, index.php
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]

        # Ensure Authorization header is passed along
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

<IfModule !mod_rewrite.c>
        # If we don't have mod_rewrite installed, all 404's
        # can be sent to index.php, and everything works as normal.
        ErrorDocument 404 index.php
</IfModule>

# Disable server signature start
        ServerSignature Off
# Disable server signature end

排查解决步骤:

  1. 检查文件权限与所有者
    Ubuntu下Apache默认运行用户是www-data,确保404的CSS文件所有者和组为www-data,权限设置正确:

    chown -R www-data:www-data public/css/
    chmod -R 644 public/css/*.css
    chmod -R 644 public/css/*.css.map
    chmod 755 public/css/
    
  2. 验证文件名大小写一致性
    CentOS文件系统不区分大小写,Ubuntu默认区分大小写。确认页面中引用的CSS文件名和实际文件名完全匹配,比如页面里写Style.Default.css但实际是style.default.css就会触发404。

  3. 修复HTTPS重写规则
    当前规则仅处理HTTP下的www跳转,且跳转目标为HTTP,易导致混合内容或跳转异常,修改为:

    # 重写www域名到主域名并强制HTTPS
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
    
    # 强制所有请求走HTTPS
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
  4. 启用RewriteBase配置
    若子域名文档根直接指向public目录,取消注释RewriteBase /:

    RewriteBase /
    
  5. 确认Apache模块与站点配置
    启用mod_rewrite并重启服务:

    sudo a2enmod rewrite
    sudo systemctl restart apache2
    

    同时确保站点Apache配置中开启AllowOverride All,否则.htaccess规则无效:

    <Directory /path/to/your/public>
        AllowOverride All
        Require all granted
    </Directory>
    

    修改后重启Apache生效。

  6. 清除浏览器缓存
    部分404可能是浏览器缓存旧跳转记录,强制刷新页面(Ctrl+F5)或清空缓存后重新测试。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 21:15:39