CodeIgniter Forums
404 Override Not Working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: 404 Override Not Working (/showthread.php?tid=68284)

Pages: 1 2


404 Override Not Working - wolfgang1983 - 06-20-2017

I this controller Notfound.php

Path: application > controllers > Notfound.php

I place it in the override 404 in routes


PHP Code:
$route['404_override'] = 'notfound'

But still does not show not overriding the 404.

I use virtual hosting with xampp

This is my htaccess

PHP Code:
Options +FollowSymLinks
Options 
-Indexes
DirectoryIndex index
.php
RewriteEngine on
RewriteCond 
$!^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond 
%{REQUEST_FILENAME} !-d
RewriteRule 
^(.*)$ index.php/$[L,QSA

I am not sure why it is not overriding it anysuggestions?

My template library is autoloaded


PHP Code:
<?php

class Notfound extends CI_Controller
{
public function 
__construct()
{
parent::__construct();
}

public function 
index()
{
$this->template->set_title('Not Found!');

$this->template->render('error/not_found'$template = array());
}

I trigger the show_404 on my controller viewthread which I have attached.


RE: 404 Override Not Working - reactionstudio - 06-20-2017

I've noticed this sometimes too, in some circumstances it won't override to your specified controller.

Can you provide us with the code which should be triggering the 404 that you expect to be overridden, and if you edit the views/errors/html/error_404.php do you see the changes you make appear on the 404 page you're seeing?


RE: 404 Override Not Working - wolfgang1983 - 06-20-2017

(06-20-2017, 01:32 AM)reactionstudio Wrote: I've noticed this sometimes too, in some circumstances it won't override to your specified controller.

Can you provide us with the code which should be triggering the 404 that you expect to be overridden, and if you edit the views/errors/html/error_404.php do you see the changes you make appear on the 404 page you're seeing?

Just attached it. To the main OP


RE: 404 Override Not Working - reactionstudio - 06-20-2017

when the 404 is triggered do you not see the default / stock codeigniter 404 page or does your notfound controller fail to load? What output are you seeing instead of the 404 page you're expecting?


RE: 404 Override Not Working - reactionstudio - 06-20-2017

also, if you navigate to your notfound controller manually what do you see?


RE: 404 Override Not Working - wolfgang1983 - 06-20-2017

(06-20-2017, 01:49 AM)reactionstudio Wrote: when the 404 is triggered do you not see the default / stock codeigniter 404 page or does your notfound controller fail to load? What output are you seeing instead of the 404 page you're expecting?

Yes the normal default 404 shows but not my override one.


RE: 404 Override Not Working - Martin7483 - 06-20-2017

Viewing the Core code, setting a custom 404_override in ./config/routes.php does not change how the show_404() method is used.

The override is used for when your Controller can not be located. It will then load the Controller that is set in the config value.
This is not what is happening in your case, because CI can locate the controller.

Look at the docs https://codeigniter.com/user_guide/general/errors.html#show_404

What you need todo is create your own show_404 method or redirect to your custom Notfound controller instead of calling show_404 and pass along any needed arguments


RE: 404 Override Not Working - wolfgang1983 - 06-20-2017

(06-20-2017, 01:52 AM)reactionstudio Wrote: also, if you navigate to your notfound controller manually what do you see?

I see the not found page when I navigate to it just the override not working


RE: 404 Override Not Working - wolfgang1983 - 06-20-2017

(06-20-2017, 02:00 AM)Martin7483 Wrote: Viewing the Core code, setting a custom 404_override in ./config/routes.php does not change how the show_404() method is used.

The override is used for when your Controller can not be located. It will then load the Controller that is set in the config value.
This is not what is happening in your case, because CI can locate the controller.

Look at the docs https://codeigniter.com/user_guide/general/errors.html#show_404

What you need todo is create your own show_404 method or redirect to your custom Notfound controller instead of calling show_404 and pass along any needed arguments

Thanks for that will look into it.


RE: 404 Override Not Working - reactionstudio - 06-20-2017

If you create the file: application/core/MY_Exceptions.php with the following code what happens?

PHP Code:
<?php


class MY_Exceptions extends CI_Exceptions {

    function 
__construct() {

        
parent::__construct();

    }

    public function 
show_404($page ''$log_error TRUE)
    {
        if (
is_cli())
        {
            
$heading 'Not Found';
            
$message 'The controller/method pair you requested was not found.';
        }
        else
        {
            
$heading '404 Page Not Found';
            
$message 'The page you requested was not found.';
        }

        
        if (
$log_error)
        {
            
log_message('error'$heading.': '.$page);
        }
        
        if( 
is_cli() ) {

            echo 
$this->show_error($heading$message'error_404'404);

        } else {
            
header("HTTP/1.0 404 Not Found");
            
            
            
header('location: http://www.yoursite.com/notfound');

        }
        exit(
4); // EXIT_UNKNOWN_FILE
    
}



dont forget to replace www.yoursite.com with your applications domain. If it works you'll need to change how the base_url is passed in but this should be enough to see if it makes progress towards a solution.