Welcome Guest, Not a member yet? Register   Sign In
Always display custom 404 error page on 404 errors
#1

[eluser]luismartin[/eluser]
I made my own 404 custom error page and changed the "404_override" config parameter to point to this page, so that if someone does, say, write a random string like this:

www.mysite.com/randomstring

the custom 404 error page comes on stage. Everything great at this point. There are other cases however in which this page is not displayed.

When I check if some request for some controller doesn't exist, I call show_404(), but it apparently only displays the typical CI error on a white page. For instance, when I perform a search:

http://www.mysite.com/search/word/2

this is the URL corresponding to the 2nd page of a search for "word". If I enter a high number corresponding to a non-existent page, the application must enter the "search" controller in order to check whether this page exist. When it checks it doesn't, AFAIK I could do the following:

- Call show_404() to display the white 404 error page.
- Redirect to my custom error 404 page, which I think should be avoided (3xx status codes which lead to a 404 are penalized in search engines).

Could I implement some kind of PHP internal redirect or switch from one controller to another without affecting the web server status?

If not, what's the answer for this?
#2

[eluser]@robertotra[/eluser]
I have solved this problem with a workaround: not only I override the route for 404 errors with a custom error controller (as you did) but also encapsulated the CI's show_404 function: inside a helper of mine (autoloaded in autoload.php) I have put my own 404 function in such a way:

Code:
function show_my_404 () {

    // I do here my output stuff
    // as inserting my own view inside a global variable
    // that will be then available inside the default error_404 page

    show_404();

}

Of course I have edited the default error_404 page with my own design to show the result there prepared..
Since the output operations are done inside the function above, the overriding 404 controller does not need to do anything but calling my 404 function.
At this point, into other controllers I do not call the default function show_404() directly but call my own 404 function 'show_my_404'.
In such a way, all 404 pages along the application are consistent and show the same results.

Cheers
Roberto
#3

[eluser]luismartin[/eluser]
Hello Roberto, I appreciate your help!

The thing is that you had to modify the core of CI. If you need to upgrade it, this will be a pitfall. There must be a way to do that by using a hook, or anything else. But I can't figure it out at this moment.
#4

[eluser]Aken[/eluser]
His method doesn't modify any core files - he is using a new helper file and a customized error page. Any conflicts that may results when upgrading would be minor and easily fixed.
#5

[eluser]@robertotra[/eluser]
[quote author="Aken" date="1328311188"]His method doesn't modify any core files - he is using a new helper file and a customized error page. Any conflicts that may results when upgrading would be minor and easily fixed.[/quote]

Thanks Aken, you replied before than myself Smile

Actually, I do not modify any core file: the default error_404.php page is inside the application folder (full path: /your_ci_folder/application/errors/error_404.php). If you overwrite not only the system folder but also the application folder when upgrading (which should not be necessary unless major changes in the overall structure - see note 1) the only care you have to take is to make a backup copy of your custom error_404.php page to replace the default one (it will be overwritten by CI's default if it will remain in the same position or you have to replace it in the new position if it will change).

Moreover, since the default error_404.php is almost a plain HTML file (it does not contain any php code other than the output of variables $heading and $message) it is likely not to change in future CI upgrades, so your changes should be safe and keep on working with them. To be sure, just test it on your test environment before going into production (a good practice to check that the whole application of yours, and not only the 404 page, is not affected by eventual upgrade problems).

Cheers
Roberto

[1] = "if" added after InsiteFX below message :)
#6

[eluser]InsiteFX[/eluser]
They do not normally change the ./application directory so all you need to do when upgrading it is copy the index.php and ./system directory over the old one's, it's that simple!
#7

[eluser]Bhashkar Yadav[/eluser]
it can also be done with .htaccess
#8

[eluser]@robertotra[/eluser]
[quote author="Bhashkar" date="1328351300"]it can also be done with .htaccess [/quote]

Seriously? When you rewrite urls passing everything to index.php file, how will you call the custom 404 page by using .htaccess? After rewriting url it has finished its assignments and its functions are lost, until the next http request. And if you do not use url rewriting to hide the index.php file, CI URI's are passed as parameters, so they are not intercepted by .htaccess as page not found.
#9

[eluser]Bhashkar Yadav[/eluser]
could you please check with
Code:
ErrorDocument 404 /controller_name/404_page

by updating into .htaccess

i hope this should help.
#10

[eluser]@robertotra[/eluser]
[quote author="Bhashkar" date="1328354527"]
ErrorDocument 404 /controller_name/404_page
[/code]
[/quote]

But this is a different issue: by use of this instructions you redirect to the CI's error controller even the pages not included inside the url rewriting.

As example, if you left out from url rewriting some folders of yours as images, script, styles, stats, blog (if you want to use wordpress on same domain, as example) and so on, when there will be a 404 http request regarding these folders, apache will show CI's 404 page instead of the default one.

Is this what you really want? I believe it is better to use as Apache 404 Page Not Found error a html copy of your CI's 404 page and not to overload the CI's error controller and the CI's error log (if activated you will have duplicate 404 error log, one for Apache and one for CI error controller).

On the other hand, if you use a global rewriting (everything to index.php) Apache will never show its own 404 error page.

Cheers
Roberto




Theme © iAndrew 2016 - Forum software by © MyBB