Joomlaで 404エラーページをカスタマイズ

 Joomlaの様なCMSを使っていれば、基本は404 Page not found.エラーが起きないはずなのだけれど、メニュー項目を再編成した場合なんかは、リンク切れが起こってしまう事もある。

404デフォルト画面

Joomlaのデフォルト404エラー画面

 Joomlaはデフォルトで、「404 – コンポーネントが見つかりません」と書かれた全く別のエラーページが表示されるが、見た目が格好悪いのとステータス200を返してしまう。

 エラーページのデザインテンプレートを無視した見た目についてはエラー時だけなので良いとしても、ステータス200を返すのは問題。検索エンジンは、そのページが正しく存在していると認識して、いつまでもインデックスされたままになってしまう。本来はステータス404を返して、ページがもう存在していないと伝える必要がある。

 Joomlaのフォーラムで話が出ていたので、404ページのカスタマイズを日本語でメモ。
http://forum.joomla.org/viewtopic.php?f=199&t=251089

 ステータスコード404を正しく返して、さらにJoomlaのメインコンテンツの部分に 404ページを表示する手順は、

  1. 魅力的な 404頁となる記事をJoomlaの記事機能で作る。セクションは未分類にしておく。
  2. その記事にリンクするメニュー項目を作る。エイリアスは、http://example.com/404.html等と分かり易くしておく。そのメニューは公開しない。メニューを使いたくない時は、次に説明するerror.phpで、echo file_get_contents(‘http://www.example.com/index.php?option=com_content&view=article&id=283′);という形式でエラーページのURLを直接書く
  3. 下に書いてある内容で、error.phpを作り、自サイト用に次の2カ所を書き換える。

    http://www.YourSite.com/404-error.htmlの部分を自サイトのエラーページのURLに変更する

    templates/YourTemplatePath/templates.cssを使っているテンプレートのCSSのパスに変更する

  4. error.phpを使っているテンプレートのディレクトリにコピーする
  5. 以上
【error.phpの内容】
<?php
/**
* @copyright   Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license      GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

if (($this->error->code) == '404') {
  echo file_get_contents('http://YourSite.com/404-error.html');
}
else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this->language; ?>"
  lang="<?php echo $this->language; ?>" dir="<?php echo $this->direction; ?>">
<head>
   <title><?php echo $this->error->code ?> - <?php echo $this->title; ?></title>
   <link rel="stylesheet" href="<?php echo $this->baseurl; ?>/
templates/YourTemplatePath/templates.css" type="text/css" />
</head>
<body>
   <div align="center">
      <div id="outline">
      <div id="errorboxoutline">
         <div id="errorboxheader"><?php echo $this->error->code ?> - <?php echo $this->error->message ?></div>
         <div id="errorboxbody">
         <p><strong><?php echo JText::_('You may not be able to visit this page because of:'); ?></strong></p>
            <ol>
               <li><?php echo JText::_('An out-of-date bookmark/favourite'); ?></li>
               <li><?php echo JText::_('A search engine that has an out-of-date listing for this site'); ?></li>
               <li><?php echo JText::_('A mis-typed address'); ?></li>
               <li><?php echo JText::_('You have no access to this page'); ?></li>
               <li><?php echo JText::_('The requested resource was not found'); ?></li>
               <li><?php echo JText::_('An error has occurred while processing your request.'); ?></li>
            </ol>
         <p><strong><?php echo JText::_('Please try one of the following pages:'); ?></strong></p>
         <p>
            <ul>
               <li><a href="<?php echo $this->baseurl; ?>/index.php"
                title="<?php echo JText::_('Go to the home page'); ?>"><?php echo JText::_('Home Page'); ?></a></li>
            </ul>
         </p>
         <p><?php
          echo JText::_('If difficulties persist, please contact the system administrator of this site.'); ?></p>
         <div id="techinfo">
         <p><?php echo $this->error->message; ?></p>
         <p>
            <?php if($this->debug) :
               echo $this->renderBacktrace();
            endif; ?>
         </p>
         </div>
         </div>
      </div>
      </div>
   </div>
</body>
</html>
<?php
}
?>

 else{ 〜 }文の箇所は、リンク先になるエラーページが見つからなかった場合に表示される安全策なので、ファイルがあると自信を持てる時は、error.phpには↓部分だけあれば、大丈夫。


// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

if (($this->error->code) == '404') {
  echo file_get_contents('http://www.YourSite.com/404-error.html');
}
?>

 これで、エラーページもきれいにメインコンテンツの部分に表示されるので、メニューにもアクセスできるし、ステータスも正しく404を返すので、SEO的にもOKになります。

あわせてこちらの記事もどうぞ

| カテゴリ:Webツールを使う |