404エラーのページをカスタマイズする

提供: GeeklogJpWiki

404 errorのページは、関数COM_handle404()で表示されています。 lib-custom.phpに、COM_handle404()のソースを元に、CUSTOM_handle404()を設置します。

さらに、.htaccessに、つぎの1行を追加してください。

ErrorDocument 404 /404.php


ブロックを表示させない

from:

$display = COM_createHTMLDocument($display, array('pagetitle' => $LANG_404[1]));

to:

$display = COM_createHTMLDocument($display, array('what' => 'none', 'pagetitle' => $LANG_404[1], 'rightblock' => false));


CUSTOM_handle404()の例

<?php

if (strpos(strtolower($_SERVER['PHP_SELF']), 'custom_handle404.php') !== false) {
    die('This file can not be used on its own!');
}

function CUSTOM_handle404($alternate_url = '')
{
    global $_CONF, $_USER, $LANG_404;


    // send 404 in any case
    header('HTTP/1.1 404 Not Found');
    header('Status: 404 Not Found');

    $display .= COM_startBlock($LANG_404[1]);
    if (isset($_SERVER['SCRIPT_URI'])) {
        $url = strip_tags($_SERVER['SCRIPT_URI']);
    } else {
        $request = $_SERVER['REQUEST_URI'];
        $url = 'http://' . $_SERVER['HTTP_HOST'] . strip_tags($request);
    }

    // Add log stuff
    if (isset($_USER['uid'])) {
        $byuser = $_USER['uid'] . '@' . $_SERVER['REMOTE_ADDR'];
    } else {
        $byuser = 'anon@' . $_SERVER['REMOTE_ADDR'];
    }
    $refurl = $_SERVER['HTTP_REFERER'];
    $remoteaddress = $_SERVER['REMOTE_ADDR'];
    $timestamp = @strftime( '%c' );
    $logentry = "404 Error generated by $byuser for url: $url - Referring url: $refurl";
    $logfile = $_CONF['path_log'] . '404.log';
    if (!$file = fopen($logfile, 'a')) {

    } else {
        fputs( $file, "$timestamp - $logentry \n" );
    }

    $display .= CUSTOM_getStaticpage('404');

    $display .= sprintf($LANG_404[2], $url);
    if ($alternate_url != '') {
        $display .= sprintf($LANG_404[4], $alternate_url);
    } else {
        $display .= $LANG_404[3];
    }
    $display .= COM_endBlock();
    // $display = COM_createHTMLDocument($display, array('pagetitle' => $LANG_404[1]));
    $display = COM_createHTMLDocument($display, array('what' => 'none', 'pagetitle' => $LANG_404[1], 'rightblock' => false));
    COM_output($display);    
    exit; // Do not want to go any further
}
個人用ツール