首页 > 解决方案 > 如果文件不存在,则重定向到文件并保留 url

问题描述

我在以下文件夹中有一个 index.php:

https://example.com/folder1/folder2/index.php

如果网址是

https://example.com/folder1/folder2/93j3h233j3

然后重定向到 index.php 但 URL 应该保持不变!

我的想法:我首先调用一个没有 .php 的 php 文件,然后尝试重定向......

RewriteRule ^folder1/folder2/([^\.]+)$ /folder1/folder2/index.php?&%{QUERY_STRING}

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^folder1/folder2/(.*)$   /folder1/folder2/index.php? [R=302]

这不起作用。并保留名字!但如何?任何想法?

谢谢

标签: .htaccess

解决方案


你可以有你的规则是这样的:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(folder1/folder2)/.+$ $1/index.php [L,NC]

无需使用您的第一条规则。


推荐阅读