如何重定向到以.htaccess的范围为子目录索引文件
我有两个网页。
我想在一个域中部署这两个页面。
当我调用root URL时,我想在root Directory中加载 index.html
,对于其他URL,我想在 index1.html >/app 目录。
这是目录结构。
www.example.com/index.html
www.example.com/app/index1.html
例如:
请求 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>
当我请求 www.example.com/signup
www.example.com/app/index1.html
时。
但是我想加载 app/index1.html
而无需重定向。请帮我。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用您显示的样本,请尝试以下内容。htaccess规则。在测试URL之前,请确保以下内容:
.htaccess
文件,index.html
和app
文件夹居住在相同root
文件夹。index1.html
/app
文件夹中的文件。With your shown samples, please try following .htaccess rules. Please make sure of following things before you test your URLs:
.htaccess
file,index.html
and yourapp
folder are residing in sameroot
folder.index1.html
file present inside/app
folder.基本上,您只需要删除最后一个
Rewriterule
指令上的r
(redirect
)标志。但这可以优化:mod_dir(即
directoryIndex
)从根中提供/index.html
。这可能已经在服务器配置中配置了,因此在这里不需要DirectoryIndex
指令。第一个
重写
指令是一种优化,以防止不必要的文件系统检查。这应该匹配要重写的文件。 IE。/app/index1.html
(不是/index.html
)。最后一个
重写
与单个字符(即。
- 点)匹配,因此排除了根目录的请求,因此每次请求root均可防止不必要的文件系统检查。另一方面,正则^(。代码> REWRITECOND
指令)。除非您使用符号链接,否则您可以删除第三条件。
根据您的URL格式,您可以使正则限制性更加限制,并删除了检查请求未映射到文件的第一个条件(文件系统检查相对昂贵)。例如。您的URL包含点吗?您给出的两个例子没有。点自然会划界文件扩展名,因此,如果您的URL不包含点,则它们自然不会映射到任何现有文件。
You basically just needed to remove the
R
(redirect
) flag on the lastRewriteRule
directive. But this can be optimised:mod_dir (ie.
DirectoryIndex
) serves/index.html
from the root. This may already be configured in the server config, so theDirectoryIndex
directive may not be required here.The first
RewriteRule
directive is an optimisation to prevent unnecessary filesystem checks. This should match the file being rewritten to. ie./app/index1.html
(not/index.html
).The last
RewriteRule
matches against a single character (ie..
- dot) so excludes requests for the root directory so prevents unnecessary filesystem checks everytime the root is requested. The regex^(.*)$
on the other hand matches everything, including the root (which fails the directory check - 2nd condition /RewriteCond
directive).Unless you are using symlinks then you can remove the 3rd condition.
Depending on the format of your URLs then you could make the regex more restrictive and perhaps remove the first condition that checks that the request does not map to a file (filesystem checks are relatively expensive). eg. Do your URLs contain dots? The two examples you gave do not. Dots naturally delimit file extensions, so if your URLs do not contain dots then they will naturally not map to any existing files.