如何让 .htaccess url 重定向并管理对 Missing.html 页面的多个调用?
我已经设置了 .htaccess 将对 mydomain.com/something.html 的调用重定向到 mydomain.com/index.php?q=something
这工作正常....但我注意到自从搬到新的托管后我得到了查询“missing”的多个查询。 我很确定这是由于错误 URL 的默认 html 页面为“missing.html”,该页面被重定向为 mydomain.com/index.php?q=missing。因此,任何丢失的 URL 都会导致我的 PHP 脚本以“丢失”作为输入运行。
有没有办法保持 URL 重定向并管理对 Missing.html 的调用,而无需实际调用我的 PHP 脚本来查找丢失的 URL?
用解决方案编辑;这是我的 .htaccess,它仅在没有现有 URL 的情况下才进行重定向:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\.html index.php?q=$1
似乎需要在之前放置 RewriteCond(如上所示)。
I have set up my .htaccess to redirect calls to mydomain.com/something.html to mydomain.com/index.php?q=something
This works fine.... but I noticed that since moving to a new hosting I am getting multiple queries for the query "missing".
I am pretty sure it is due to the default html page for wrong URLs being "missing.html" which is redirected as mydomain.com/index.php?q=missing. So any missing URL will cause my PHP script to be run with "missing" as input.
Is there a way to keep the URL redirect and manage the calls to missing.html without actually calling my PHP script for missing URLs?
Edit with solution; here is my .htaccess which does the redirect only if there is no existing URL:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\.html index.php?q=$1
Seems the RewriteCond need to be placed before (as shown above).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是否可以添加一条重写规则,该规则优先于其他规则,并专门将 mydomain.com/missing.html 重写到 404 页面的位置? (查看您已经设置的重定向规则会有所帮助。)
Isn't is possible to add a rewriting rule which takes precedence over the other one and specifically rewrites mydomain.com/missing.html to the location of your 404 page? (Seing the redirection rule you already set up would help.)
您正在寻找
RewriteCond
。在您的
.htaccess
中尝试一下:这些规则也将停止重写任何可访问路径。如果您想要严格
missing.html
,那么以下规则就足够了:请注意,这些指令必须位于
RewriteRule
之前。You're looking for
RewriteCond
.Try this in your
.htaccess
:These rules will also stop rewriting for anything is an accessible path. If you want strictly
missing.html
, then the following rule will be sufficient:Note these directives must come before
RewriteRule
.