发新话题
打印

给Sablog加上自定义错误页面(401、403、404)

给Sablog加上自定义错误页面(401、403、404)

应用系统:Sablog 1.6
修改文件:2个 + .htaccess
添加文件:1个模板 + 若干个错误图片
前提条件:
1) .htaccess生效
2) Rewrite Rule生效
3) 需要一定的PHP和HTML基础
原贴:http://www.snailium.net/show-110-1.html

步骤:

第一步:修改 /index.php
找到:
复制内容到剪贴板
代码:
require_once PrintEot('index');
在上方添加:
复制内容到剪贴板
代码:
// 自定义错误
elseif ($_GET['action'] == 'error') {
   $error_code = $_GET['code'];
   $pagefile = 'error';
}
第二步:修改模板文件夹下的 index.php
找到:
复制内容到剪贴板
代码:
<head>
在下方添加:
复制内容到剪贴板
代码:
<!--
EOT;
if ($pagefile == 'error') {
  print <<<EOT
-->
<base href="$options[url]" />
<!--
EOT;
}
print <<<EOT
-->
第三步:在模板文件夹下建立 error.php
内容如下:
复制内容到剪贴板
代码:
<!--<?php
if(!defined('SABLOG_ROOT')) {
    exit('Access Denied');
}
print <<<EOT
-->
<div class="content-top">
<!--
EOT;
switch ($error_code)
{
    case '400':
      print '-->  错误 400:不正确的请求<!--';
      break;
    case '401':
      print '-->  错误 401:需要身份验证<!--';
      break;
    case '403':
      print '-->  错误 403:禁止访问<!--';
      break;
    case '404':
      print '-->  错误 404:文件不存在<!--';
      break;
    case '500':
      print '-->  错误 500:服务器内部错误<!--';
      break;
    default:
      print '-->  未知错误<!--';
      break;
}
print <<<EOT
-->
</div>
<div id="article-box">
  <div class="article-title" style="text-align: center;">
<!--
EOT;
switch ($error_code)
{
    case '401':
      print '-->    <img src="/images/error/401.jpg" /><!--';
      break;
    case '403':
      print '-->    <img src="/images/error/403.jpg" /><!--';
      break;
    case '404':
      print '-->    <img src="/images/error/404.gif" /><!--';
      break;
    default:
      print '-->    <img src="/images/error/general.gif" /><!--';
      break;
}
print <<<EOT
-->
  </div>
</div>
<div class="article-content" style="text-align: center;">
  <p><a href="$options[url]">返回首页</a>&nbsp;&nbsp;&nbsp;&nbsp;
  <a href="javascript: history.go(-1);">返回上页</a></p>
</div>
<!--
EOT;
?>
第四步:上传相应的错误提示图片到 /images/error/

第五步:修改 .htaccess
先确认 Rewrite rule 生效,然后在文件尾部加入下面几行。
复制内容到剪贴板
代码:
# 自定义错误
RewriteRule ^error-([0-9]+)\.html$ index.php?action=error&code=$1
ErrorDocument 400 /error-400.html
ErrorDocument 401 /error-401.html
ErrorDocument 403 /error-403.html
ErrorDocument 404 /error-404.html
ErrorDocument 500 /error-500.html
完成!

TOP

发新话题