如何在浏览器地址栏中将根目录(/)重定向到指定的目录索引页

前端开发 2026-07-09

我们有 website.com/,它的主页被指定为 /home.html。我们需要任意访问 / 的浏览器因此进行重定向,并呈现 /home.html

也就是说,在客户端浏览器的地址栏中:

www.website.com/

将显示为

www.website.com/home.html

但我们无法让它实现。

我们可以在 Mod_rewrite 中让它工作,但那不是我们需要的,我们需要浏览器地址栏显示主页名称。

在这里我们有:

Options -Indexes

# (1)
# Redirect 301 / /home   
# (2)
# Redirect 301 /$ /home  

RewriteEngine On
DirectoryIndex home.html

# (3)
# Redirect 301 / /home
  1. 它会循环并返回 www.website.com/homehomehomehomehomehomehomehome ....我们只需要在路径为空时执行一次
  2. 这似乎不起作用
  3. 这似乎也不起作用,页面 www.website.com/ 仍然留在那里,并不会切换到 website.com/home

解决方案

你可以在.htaccess里添加一条规则

Options -Indexes

RewriteEngine On

# Redirect only the root URL
RewriteRule ^$ /home.html [R=301,L]

DirectoryIndex home.html
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章