【发布时间】:2021-05-14 08:50:33
【问题描述】:
我目前有以下 htaccess 项目来回交换 html 和 htm 文件扩展名,因此如果您尝试加载 index.html 但唯一存在的文件是 index.htm 它将代替它。反之亦然。
我们的目标是将所有内容都迁移到 PHP,但与此同时,是否有可能将其扩展到 PHP。因此,如果较旧的 html 页面之一调用 index.htm 或 index.html,它会发现它们不存在并改为提供 index.php。同样,如果您键入 index.php 并且它不存在,它将提供 htm 或 html 文件。
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)(?:\.(?:htm))?$ /$1.html [L,NC,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.htm -f [NC]
RewriteRule ^(.+?)(?:\.(?:html))?$ /$1.htm [L,NC,R=302]
类似于.htaccess: rewrite .htm urls internally to .php, but also redirect .php urls to .htm,但稍微复杂一些。
【问题讨论】:
-
您使用的是哪个版本的 Apache?这些是您使用的实际指令吗?
-
@MrWhite Apache 2.4.46,是的,这就是当前正在使用的相应地交换 html 和 htm。
标签: html .htaccess url-rewriting