使用 .htaccess Mod_rewrite 隐藏仅适用于缺失页面的 .php 扩展名

发布于 2024-12-31 21:24:37 字数 695 浏览 0 评论 0原文

请原谅我,因为我对使用 .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 技术交流群。

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

发布评论

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

评论(1

君勿笑 2025-01-07 21:24:37

I think for your first rule, you want something along the lines of:

RewriteBase /
RewriteRule ^([^.]+)$ $1.php [L]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文