skip to Main Content

I’ve made a custom 404.php page and tried to make a properly installed 404 redirect in .htaccess.

ErrorDocument 400 /400/index.php
ErrorDocument 401 /401/index.php
ErrorDocument 403 /403/index.php
ErrorDocument 404 /404/index.php
ErrorDocument 405 /405/index.php
ErrorDocument 408 /408/index.php
ErrorDocument 414 /414/index.php
ErrorDocument 500 /500/index.php
ErrorDocument 501 /501/index.php
ErrorDocument 502 /502/index.php
ErrorDocument 503 /503/index.php
ErrorDocument 504 /504/index.php

It works fine and i see my 404 page, when trying to type incorrect links.

But, when i use different website analyzers or seo checkers, i see the same issue with not properly installed 404 page:
Not properly installed 404 page
Not properly installed 404 page

404.php page with this code before <!Doctype>

<?php
header('HTTP/1.1 404 Not Found');
header("Status: 404 Not Found");
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();

What can i do wrong? Hosting technical support told me, that everything is properly installed on their side, but they can’t help with SEO questions. Thanks in advance

2

Answers


  1. Chosen as BEST ANSWER

    I've just found a solution. Website for 404 error checker https://websiteadvantage.com.au/404-Error-Handler-Checker gave me such result:

    https://phpout.com/wp-content/uploads/2023/05/7JRXC.png

    The problem was in firstly redirecting a page, and only then sending a

    http_response_code(404);
    

    So i put this:

    function noPageFound($domain) {
            http_response_code(404);
            echo"<html><head><meta http-equiv='Refresh' content='0; ....>"; exit();
        }
    

    And deleted 404 response code in 404.php page, as it was not neccessary.

        <?php
    header('HTTP/1.1 404 Not Found');
    header("Status: 404 Not Found");
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    session_start();
    

    And the result was perfect. Thanks everyone.


  2. It sounds like the issue may be related to the headers you are sending in your 404.php file. When you use the header() function to set the HTTP response code to 404, you should not also use the Status header, as it can lead to conflicts.

    Try removing the Status header and simplifying your code to:

    <?php
    http_response_code(404);
    session_start();
    ?>
    <!DOCTYPE html>
    <html>
    <head>
      <title>404 Not Found</title>
      <!-- Include any necessary CSS and JavaScript files here -->
    </head>
    <body>
      <!-- Your custom 404 page content here -->
    </body>
    </html>
    

    Additionally, keep in mind that SEO checkers may be caching previous responses, so it’s possible that even after you’ve fixed the issue, it may take some time for the checkers to recognize the changes.

    To redirect all 404 error responses to a specific URL using .htaccess, you can use the following code:

    RewriteEngine On
    ErrorDocument 404 /your-specific-url
    

    This code turns on the RewriteEngine and sets the ErrorDocument directive to redirect all 404 errors to the specified URL (replace "your-specific-url" with the actual URL you want to redirect to).

    Make sure to place this code at the beginning of your .htaccess file, before any other rewrite rules, to ensure that all 404 errors are caught and redirected.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search