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

如何为子目录配置.htaccess文件?

问题描述

我原本在根目录使用以下.htaccess配置时一切正常:

DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA]

但当我创建了名为api的目录,把所有脚本迁移到该目录后,页面显示空白,无法正常工作。我修改后的.htaccess配置如下:

DirectoryIndex index.php
RewriteEngine on
RewriteBase /api/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /api/index.php [QSA]
修复方案
  • 确认文件位置:确保修改后的.htaccess文件放在/api/目录下,而非根目录,否则规则无法匹配目录内的请求。
  • 调整RewriteRule路径:利用已设置的RewriteBase /api/,把目标路径改为相对路径,同时添加L标志终止后续规则处理,避免冲突:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /api/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
  • 排查PHP执行错误:空白页多是PHP报错但被屏蔽导致,可在/api/index.php顶部临时添加代码开启错误显示:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

也可以直接查看服务器的PHP错误日志,确认是否是脚本迁移后路径依赖、文件权限等问题引发的执行失败。

  • 验证服务器配置:确保Apache已开启mod_rewrite模块,且服务器配置中对/api/目录设置了AllowOverride All,否则.htaccess规则不会生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 04:00:59