我正在尝试使用.htaccess文件将目录中的任何文件或子文件夹重定向到一个文件,同时仍保留原始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]
这将在不更改浏览器中的URL的情况下将/folder1/foo/bar
重定向到/folder1/index.php
。
上面的RewriteCond
确保你不会将/folder1/index.php
重写为自己(/folder1/index.php
),否则规则会导致无限循环错误。
你可以做:
RedirectMatch 301 ^https://example.com/folder1/ https://example.com/folder1/index.php
这允许您从模式中的第一个URL重定向到第二个URL