【问题标题】:Does not have a custom 404 Error Page没有自定义 404 错误页面
【发布时间】:2016-08-15 15:16:29
【问题描述】:

我已经使用 cakephp 控制器设置了 404 页面,当我尝试转到一个不存在的页面时它工作正常,它返回 http 响应 404 并显示 404 页面,但是像 woorank.com 和PowerSeo 总是说没有定义自定义的 404 页面?

我可以在 .htaccess 文件中做些什么来让这些工具知道我定义了 404 页面吗?我已将 ErrorDocument 404 /404.html 添加到 .htaccess 但仍然是相同的错误。他们在哪里寻找定义的 404 页面?

.htaccess 文件

<IfModule mod_rewrite.c>
       RewriteEngine on
       RewriteCond %{HTTP_USER_AGENT} libwww-perl.* 
       RewriteRule .* – [F,L]
       RewriteCond %{SERVER_PORT} 80 
       RewriteRule ^(.*)$ https://www.domain.com [L,R=301]
      </IfModule>

    ExpiresActive On
    ExpiresByType text/html "access plus 1 day"
    ExpiresByType image/gif "access plus 10 years"
    ExpiresByType image/jpeg "access plus 10 years"
    ExpiresByType image/png "access plus 10 years"
    ExpiresByType text/css "access plus 10 years"
    ExpiresByType text/javascript "access plus 10 years"
    ExpiresByType application/x-javascript "access plus 10 years"
Header unset Pragma
Header unset ETag
FileETag None
ErrorDocument 404 /404.html

【问题讨论】:

    标签: html cakephp


    【解决方案1】:

    来自CakePHP Book:

    按照惯例,如果存在的话,CakePHP 将使用 App\Controller\ErrorController。实现此类可以为您提供一种自定义错误页面输出的免配置方式。

    Here is answer如何创建ErrorController

    接下来添加你的路由器:

    CakePHP 2.x:

    Router::parseExtensions('html');
    Router::connect(
        '/404.html',
        array('controller' => 'errors', 'action' => 'error404'),
    );
    

    CakePHP 3.x:

    Router::scope('/', function ($routes) {
        $routes->extensions(['html']);
        $routes->connect(
            '/404.html',
            ['controller' => 'Errors', 'action' => 'error404'],
        );
    });
    

    【讨论】:

      猜你喜欢
      • 2016-04-25
      • 2013-09-16
      • 2015-03-18
      • 1970-01-01
      • 2011-07-25
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多