.htaccess - 如何在保留原始 URL 的同时使用 .htaccess 进行重定向
问题描述
我正在尝试使用 .htacces 文件将目录中的任何文件或子文件夹重定向到一个文件,同时仍保留原始 URL 输入。
因此,如果用户访问:
https://example.com/folder1/folder2/folder3/
or
https://example.com/folder1/folder2/file.php
它会将它们重定向回:
https://example.com/folder1/index.php
但原始 URL 输入不会改变。
解决方案
您可以使用RewriteRule
. 在文档根目录的 htaccess 中添加以下规则:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder1/index\.php$ [NC]
RewriteRule ^folder1/.+$ /folder1/index.php [L]
这将重定向/folder1/foo/bar
到/folder1/index.php
而不更改浏览器中的 url。
以上RewriteCond
确保您不会重写/folder1/index.php
自身 ( /folder1/index.php
),否则该规则可能会导致无限循环错误。
推荐阅读
- java - 模拟 - 创建新文件 (Java)
- java - 如何在 Python 解释器中构造 Scala 映射并作为 arg 传递给 Scala 构造函数?
- html - 3张背景图片,每张占横幅的1/3,如何在固定高度的同时响应裁剪每个宽度
- node.js - 节点邮件程序无法为 50 个中的 1 个收件人发送电子邮件
- javascript - 我的 React 应用程序中的 Axios 出现未知 SyntaxError
- jupyter-notebook - Jupyter Notebook 和 md5sum 是否存在任何已知问题?
- android - 在动态生成的 TextView 中使用 StartActivityForResult
- c++ - gcc 10.1:clang-tidy (clion) 和预编译头文件 (cmake) 的奇怪行为
- sql - 对连接管理器的 AcquireConnection 方法调用失败,错误代码为 0xC0202009
- mysql - mysql OR 不会使用索引