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

Yii2伪静态URL返回404错误,迁移至Ubuntu22.04后异常

问题:Yii2应用迁移至Ubuntu22.04后伪静态URL返回404

将正常运行的Yii2应用从Ubuntu 14.04虚拟机迁移至Ubuntu 22.04虚拟机(全新搭建,未通过系统升级),两台服务器PHP版本一致。伪静态URL http://(server)/site/login 返回404错误,Apache2访问日志有对应404记录,但错误日志无内容。代码通过GitHub同步,配置已尽量复刻原服务器,原环境下该URL可正常访问。

相关配置信息

Yii2 urlManager配置

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
    ],
],

Web目录下.htaccess配置

# Requires FollowSymLinks to work; also turn on the REWRITE engine
<IfModule mod_rewrite.c>
   Options +FollowSymlinks
   RewriteEngine On
</IfModule>

# Unless explicit file or director exists, redirect all requests to Yii entry script
<IfModule mod_rewrite.c>
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule . index.php
</IfModule>

AddType application/octet-stream .txt

Apache虚拟主机配置(dbos.conf)

<VirtualHost *:80>
    ServerAdmin jmdemoor@localhost
    ServerName beretania
    DocumentRoot /var/www/dbos/web
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/dbos/web/>
        Options Indexes FollowSymLinks Multiviews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Apache全局配置(apache2.conf)片段

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
...
AccessFileName .htaccess

解决思路

  • 启用mod_rewrite模块:Ubuntu22.04的Apache默认未启用rewrite模块,执行命令 sudo a2enmod rewrite,然后重启Apache服务:sudo systemctl restart apache2。
  • 更新访问控制语法:Apache2.4不再支持Order allow,deny和allow from all,将虚拟主机配置中对应行替换为Require all granted,避免权限限制导致rewrite规则不生效。
  • 移除Multiviews选项:虚拟主机配置里的Multiviews可能与rewrite规则冲突,修改为Options Indexes FollowSymLinks后重启Apache。
  • 检查.htaccess权限:确保文件所有者为www-data,权限设置为644,执行命令 sudo chown www-data:www-data /var/www/dbos/web/.htaccess 和 sudo chmod 644 /var/www/dbos/web/.htaccess。
  • 测试rewrite规则有效性:在.htaccess中添加临时规则 RewriteRule ^test$ index.php,访问http://(server)/test,若能正常跳转则说明rewrite模块工作正常,问题可能出在Yii2配置或其他环节。
  • 提升日志级别排查:在虚拟主机配置中添加LogLevel debug,重启Apache后重新访问404地址,查看错误日志获取更多细节。

内容的提问来源于stack exchange,提问作者Papa Joe Dee

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 09:35:31