【问题标题】:Filtering pages to redirect to 404 in .htaccess过滤页面以重定向到 .htaccess 中的 404
【发布时间】:2014-03-15 19:15:07
【问题描述】:

我有一个网站,它曾经有一个搜索引擎脚本,该脚本已被删除并替换为新内容。

网站经常被以下请求淹没:

/index.php?page=search/web/appdojo.com

/index.php?page=search/web/2tu.us

/index.php?page=search/emailafriend&url=http%253A%252F%252F

/index.php?page=user/viewcomments/images/82e10127b84ee2750c

这会导致大量进程启动,然后我们会遇到托管服务提供商的问题。

我想我应该能够在调用index.php 之前在.htaccess 中停止这些,但还没有想出一个regex 表达式来做到这一点,而是让index.php url 的其余部分顺利通过。

有人可以帮忙吗? -谢谢

【问题讨论】:

    标签: php regex .htaccess redirect http-status-code-404


    【解决方案1】:

    由于您在标签中包含 PHP,因此在 index.php 上简单地显示 404 错误并使其看起来像是错误会容易得多。所以你可以把它放在 index.php 页面上的 everything 之前:

    if (isset($_GET['page'])) {
        header('HTTP/1.0 404 Not Found');
        include('404.php'); //Whatever your 404 error page is
        exit();
    }
    

    【讨论】:

    • 我已经尝试过了,但是 index.php 启动了一个进程,我需要阻止它运行 index.php 文件。我想我必须在 .htaccess 中这样做?
    • exit(); 应该杀死其他一切。
    【解决方案2】:

    如果您的索引页面根本没有使用 page 变量,您可以尝试阻止所有在 url 中包含 page 参数的 url

    RewriteRule ^index.php?page= /Your_404_page.php
    

    【讨论】:

      【解决方案3】:

      是的,最好在 .htaccess 阶段停止它们以防止加载 PHP。以下是DOCUMENT_ROOT/.htaccess 文件中适用于您的规则:

      RewriteEngine On
      
      RewriteCond %{THE_REQUEST} /index\.php\?page= [NC]
      RewriteRule ^ - [R=304,L]
      

      【讨论】:

        猜你喜欢
        • 2011-12-05
        • 1970-01-01
        • 2013-01-31
        • 1970-01-01
        • 2016-11-21
        • 2014-12-23
        • 1970-01-01
        • 2013-06-09
        • 2023-03-10
        相关资源
        最近更新 更多