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

如何通过.htaccess将子目录index.html设为Apache站点根索引?

站点根索引配置方案

服务器目录结构

/html
  .git
  project-folder
    build
      index.html
  .htaccess

Apache已将/html设为https://example.com的站点根目录,需配置.htaccess让project-folder/build/index.html成为域名的根索引文件,同时明确两个问题:

  1. DirectoryIndex能否解决该问题?
  2. 若使用Rewrite规则,对应的RewriteRule和RewriteCond该如何编写?

解决方案

1. 使用DirectoryIndex(最简方案)

DirectoryIndex完全可以解决这个问题,它是Apache专门用来指定目录默认索引文件的指令,直接填写目标文件的相对路径即可:

DirectoryIndex project-folder/build/index.html

当用户访问https://example.com/时,Apache会自动加载这个指定的文件作为默认索引。

2. 使用Rewrite规则(替代方案)

如果需要通过Rewrite模块实现相同效果,需先开启Rewrite引擎,再配置对应条件和规则:

RewriteEngine On

# 仅匹配根目录请求
RewriteCond %{REQUEST_URI} ^/$
# 将根路径转发到目标索引文件
RewriteRule ^$ project-folder/build/index.html [L]
  • RewriteEngine On:开启URL重写功能
  • RewriteCond %{REQUEST_URI} ^/$:限定仅对根目录的请求生效
  • RewriteRule ^$ project-folder/build/index.html [L]:完成请求转发,[L]表示这是最后一条规则,停止后续规则匹配

如果需要覆盖所有目录请求(包括根目录),可以用更灵活的写法:

RewriteEngine On

# 匹配真实存在的目录请求
RewriteCond %{REQUEST_FILENAME} -d
# 转发到目标索引文件
RewriteRule ^ project-folder/build/index.html [L]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 08:19:52