CakePHP

CakePHP – Redirect 404 Not Found Errors to the Homepage

For some reason, you may want to redirect 404 not found error to the homepage in CakePHP website. In order to do this 2 files have to be edited/created.

1. Edit or create /app/Lib/Error/AppExceptionRenderer.php and insert the following codes

<?php
App::uses('EceptionRenderer', 'Error');
class AppExceptionRenderer extends ExceptionRenderer {
    public function error400($error) {
        return $this->controller->redirect('/');
    }

}

2. Edit /app/Config/Core.php find the following code

'renderer' => 'ExceptionRenderer',

and replace it with

'renderer' => 'AppExceptionRenderer',

3. Disable debugging by editing /app/Config/Core.php. To disable find the following code

Configure::write('debug', 2);

and change to

Configure::write('debug', 0);

Redirect Default CakePHP Pagination URL to SEO Friendly URL with .htaccess

In the previous post, I post a guide on how to create SEO friendly URL in CakePHP pagination.

However, if you already use default CakePHP pagination prior to that, you want to redirect the old pagination URL to the new SEO friendly URL.

I’m using .htaccess file to accomplish this.

Let’s take an example, by default we will have pagination URL like this

http://www.website.com/post
http://www.website.com/post/page:2
http://www.website.com/post/page:3
...

we would like to redirect to

http://www.website.com/post
http://www.website.com/post/page/2
http://www.website.com/post/page/3
...

To redirect, we have to edit .htaccess file located at /app/webroot/.htaccess. Next, add the following code.

RewriteEngine On
RewriteBase /
RewriteRule ^(.+)/page:([0-9]+)/$ /$1/page/$2/ [R=301,L]