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

Apache WordPress子目录.htaccess报错:DirectoryIndex不被允许

问题:本地克隆WordPress站点静态子目录访问异常

问题现象

线上正常运行的WordPress站点包含旧静态文件子目录classic,本地用Ubuntu 22.04+XAMPP搭建克隆环境后,WordPress主体功能正常,但classic子目录访问出现两种矛盾错误:

  1. 保留子目录.htaccess时,访问http://site.local/classic/触发错误:

[Sun Feb 26 17:36:06.779271 2023] [core:alert] [pid 81557] [client 127.0.0.1:37060] /srv/www/site/classic/.htaccess: DirectoryIndex not allowed here
子目录.htaccess内容与线上一致:

DirectoryIndex index.html
  1. 删除子目录.htaccess后,访问http://site.local/classic/触发错误:

[Sun Feb 26 17:42:03.345577 2023] [autoindex:error] [pid 93553] [client 127.0.0.1:48984] AH01276: Cannot serve directory /srv/www/site/classic/: No matching DirectoryIndex (index.php) found, and server-generated directory index forbidden by Options directive
但直接访问http://site.local/classic/index.html可正常打开。

本地环境配置

httpd-vhosts.conf 相关片段

<VirtualHost *:80>
       DocumentRoot "/srv/www/site/"
       ServerName site.local
    <Directory /srv/www/site>
        Options FollowSymLinks
        AllowOverride Limit Options FileInfo
        DirectoryIndex index.php
        Require all granted
    </Directory>
    <Directory /srv/www/site/wp-content>
        Options FollowSymLinks
        Require all granted
    </Directory>
</VirtualHost>

站点根目录.htaccess(WordPress默认)

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

解决建议

  • 调整本地httpd-vhosts.conf权限:本地根目录的AllowOverride未包含Indexes权限,而DirectoryIndex属于Indexes类指令,导致子目录.htaccess无法使用该指令。修改根目录的AllowOverride配置,添加Indexes或直接设为All:
<Directory /srv/www/site>
    Options FollowSymLinks
    AllowOverride All
    DirectoryIndex index.php
    Require all granted
</Directory>

修改后重启Apache,子目录的DirectoryIndex指令即可生效。

  • 模拟线上无httpd-vhosts.conf修改权限的场景:在子目录.htaccess中用Rewrite规则替代DirectoryIndex,实现目录访问自动指向index.html:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.html [L]
</IfModule>

此方案无需修改主配置,仅通过子目录.htaccess即可解决问题。

  • 核心原因说明:线上环境能正常使用子目录的DirectoryIndex,是因为线上主机的AllowOverride权限默认包含Indexes,而本地XAMPP的默认配置未开启该权限,这是两地环境的核心差异。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 06:27:32