【问题标题】:Declare 404 error even if the page does exist即使页面确实存在也声明 404 错误
【发布时间】:2018-03-22 01:22:09
【问题描述】:

我正在尝试“CheckBot”并且我有一个观察结果。问题是,在我的 PHP 代码中,我有一个 if 来检查 URL 是否存在于名为“seo_url”的表中,如果它不存在,那么我们将显示 error.tpl 的内容

<?php 

require_once 'app/config.php';

$url = isset($_GET["url"])?$_GET["url"]:'index';

$querytcseos = DB::queryFirstRow("SELECT * FROM tc_seos WHERE seo_url = %s LIMIT 1", $url);

  require_once 'includes/header.tpl';

  if (!$querytcseos['seo_file']) {
    include 'includes/error.tpl';
    } else {
    include 'includes/'.$querytcseos['seo_file'].'.tpl';
  }

  require_once 'includes/footer.tpl';

?>

如果您正在查找内容,当您输入 example.com/page-not-fount 时,我发现这似乎不是正确的做法,因为“CheckBot”。

如何将我的错误页面视为错误 404?

【问题讨论】:

  • 您的问题在某些部分不明确,但最终如果您想在应用程序级别发送 404,您只需向客户端发送 404 响应代码,例如http_response_code(404).
  • @Marty 然后我可以放 在 error.tpl 中,机器人会知道这是 404 错误页面吗?
  • 不,不是那样的 - 实际上只是在脚本顶部调用http_response_code(404)。您可以在 Chrome 的网络工具中检查服务器发送的响应代码。
  • @Marty 谢谢,这似乎有效!

标签: php


【解决方案1】:
    http_response_code(404);

最佳实践。你应该这样响应它,让服务器(Apache/Nginx/IIS)来处理 404 页面。

【讨论】:

    猜你喜欢
    • 2018-08-20
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    相关资源
    最近更新 更多