简体   繁体   中英

Php error log 404 and redirect to home page

I have this code below that handles 404 errors but i like to redirect all the 404 errors to home page, here is what i have:

function show_404($page = '', $log_error = TRUE)
{
    $heading = "404 Page Not Found";
    $message = "The page you requested was not found.";

    // By default we log this, but allow a dev to skip it
    if ($log_error)
    {
        log_message('error', '404 Page Not Found --> '.$page);
    }

    echo $this->show_error($heading, $message, 'error_404', 404);
    exit;
}

How can i redirect to home page instead of giving 404 error.

Do NOT return anything but a 404 status for a non-existant resource. Search engine crawlers and other automated utilities will function incorrectly at best, and DoS your site offline at worst.

The only case in which issuing a redirect from a 404 response would be considered acceptable is in the case of an actual user with an actual browser, and in this case you can use a <meta> redirect to achieve the same effect without breaking HTTP.

eg:

<meta http-equiv="refresh" content="0;URL='/index.php'" />

Just serve a lightweight page containing the directive in the <head> section and you will avoid 100% of the doom and gloom I've predicted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM