删除扩展名后,如果文件不存在,则发送到特定页面

发布于 2025-01-10 10:33:52 字数 562 浏览 2 评论 0原文

我的问题有点类似于 这个但不完全是,我无法充分理解它以适应我的需要。

我有这个 htaccess 规则来删除效果很好的文件扩展名,

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

所以 /somepage 指的是 /somepage.php。我现在需要一种后备方案,以防 somepage.php 不存在。

在这种情况下, /somepage 将重写为 /catchall.php?link=somepage ,它可以处理请求。我该怎么做?

My question is somewhat similar to this one but not exactly and I can't understand it enough to adapt it to my needs.

I have this htaccess rule to remove file extensions which works well

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

So /somepage refers to /somepage.php. I now need a sort of fallback to it incase somepage.php doesn't exist.

In which case /somepage would rewrite to /catchall.php?link=somepage which can handle the requests. How can I do this?

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

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

发布评论

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

评论(1

云醉月微眠 2025-01-17 10:33:52

您当前的规则是盲目添加 .php 扩展名,而不检查是否存在匹配的 .php 文件。您可以调整规则来检查 .php 文件是否存在,然后仅添加 .php。当匹配的 .php 文件不存在时,我们可以重写为 /catchall.php

RewriteEngine On

# add .php extension only if a matching .php file exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# If URI is still not pointing to a valid file/directory
# then rewrite to /catchall.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ catchall.php?link=$0 [L,QSA]

Your current rule is adding .php extension blindly without checking existence of matching .php file. You can tweak your rule to check for presence of .php file and then only add .php. When a matching .php file doesn't exist then we can rewrite to /catchall.php.

RewriteEngine On

# add .php extension only if a matching .php file exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# If URI is still not pointing to a valid file/directory
# then rewrite to /catchall.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ catchall.php?link=$0 [L,QSA]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文