如何使用 .htaccess 将根路由重定向到子目录索引文件

BTS*_*TSM 6 .htaccess mod-rewrite redirect url-rewriting server

我有两个网页。我想将这两个页面部署在一个域上。当我调用根URL时,我想加载index.html根目录中的,而对于其他URL,我想加载目录index1.html中的/app

这是目录结构。

www.example.com/index.html
www.example.com/app/index1.html
Run Code Online (Sandbox Code Playgroud)

例如:请求www.example.com 加载时index.html

用于www.example.com/login 装载/app/index1.html

用于www.example.com/signup 装载/app/index1.html

这是我尝试过的。

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule ^(.*)$ /app/index1.html [R=301,L]
</IfModule>

Run Code Online (Sandbox Code Playgroud)

www.example.com/signup当我请求时,这会进行重定向www.example.com/app/index1.html

但我想加载app/index1.html而不重定向。请帮我。

Rav*_*h13 5

对于您显示的示例,请尝试遵循 .htaccess 规则。在测试您的网址之前,请确保以下事项:

  • 确保您的.htaccess文件index.htmlapp文件夹位于同一root文件夹中。
  • 确保文件夹index1.html内有文件/app
  • 确保在测试 URL 之前清除浏览器缓存。

RewriteEngine ON
##Rule from OP's attempt to block direct access of index.html file.
RewriteRule ^index\.html$ - [NC,L]

##Rule to handle only url www.example.com here.
RewriteRule ^/?$  /index.html [QSA,NC,L]

##Rules to handle rest of the cases here..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^  /app/index1.html [QSA,NC,L]
Run Code Online (Sandbox Code Playgroud)