仅当文件存在时才将 .html 映射到 .php

发布于 2024-09-10 11:08:44 字数 248 浏览 1 评论 0原文

这是我的规则:

RewriteRule ^(.*)\.html$ /$1.php

这适用于服务器上有匹配的物理 .php 文件的“虚拟”.html url。

但是,当我输入实际 .html 文件的 url 时,我无法访问它,因为上面的规则当然是告诉服务器查找物理 .php 文件。

那么如何修改规则以仅在存在物理 .php 文件时才应用于 URL,否则按原样加载 .html 文件。

Here is my rule:

RewriteRule ^(.*)\.html$ /$1.php

This works for "virtual" .html urls which have a matching physical .php file on the server.

However when I type in a url for an actual .html file, I cannot access it, because of course the rule above is telling the server to look for a physical .php file.

So how can the rule be modified to apply to URLs only if there is a physical .php file, else load the .html file as is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染火枫林 2024-09-17 11:08:44

试试这个规则:

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)\.html$ /$1.php

Try this rule:

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)\.html$ /$1.php
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文