使用 .htaccess Mod_rewrite 隐藏仅适用于缺失页面的 .php 扩展名
请原谅我,因为我对使用 .htaccess 还很陌生,并且我遇到了一些问题,因为我需要的设置尽管很简单,但似乎很少见。
基本上,我正在研究一个辅助域,试图隐藏页面中的 .php 扩展名,同时还将对不存在页面的请求重定向到自定义搜索页面。总体想要的效果是:
realpage => realpage.php
和
falsepage => search.php?q=falsepage
到目前为止,我的代码(见下文)似乎正确应用了后者,但不是为“domain.com/realpage”返回“realpage.php”,而是返回 404 错误。
我发现了一些变体,它们也会将 realpage 移动到“search.php?q=missing”,以防给出任何可能出现问题的指示!
我目前的 .htaccess 文件:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^([^\.]+)$ $1.php [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)\.php$ search.php?q=$1 [L]
感谢您提供的任何帮助!非常感谢!
Please excuse me as I'm pretty new to using .htaccess, and am having a few issues with it as the setup I need is, despite being simple, seemingly rare.
Basically, I'm working on a secondary domain trying to hide .php extensions from pages, but whilst also redirecting requests for nonexistent pages to a custom search page. The overall desired effect is:
realpage => realpage.php
and
falsepage => search.php?q=falsepage
So far, the code I have (see below) seems to apply the latter correctly, but rather than returning 'realpage.php' for 'domain.com/realpage' it returns a 404 error.
I've found variations which also move realpage to 'search.php?q=missing' in case that gives any indication what might be going wrong!
My .htaccess file at the moment:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^([^\.]+)$ $1.php [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)\.php$ search.php?q=$1 [L]
Thanks for any help you can offer! It's very much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为对于你的第一条规则,你想要一些类似的东西:
我通过 重写规则测试器< /a>,它将把“realpage”定向到“realpage.php”。如果这不起作用,我建议禁用第二个规则,确保第一个规则运行良好,然后添加对丢失文件的检查。
I think for your first rule, you want something along the lines of:
I ran that through the rewrite rule tester, and it will direct 'realpage' to 'realpage.php'. If that doesn't work, I would suggest disabling your second rule, making sure the first one works well, then adding the check for missing files in later.