删除扩展名后,如果文件不存在,则发送到特定页面
我的问题有点类似于 这个但不完全是,我无法充分理解它以适应我的需要。
我有这个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您当前的规则是盲目添加
.php
扩展名,而不检查是否存在匹配的.php
文件。您可以调整规则来检查.php
文件是否存在,然后仅添加.php
。当匹配的.php
文件不存在时,我们可以重写为/catchall.php
。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
.