![]() |
How do I handle invalid controllers - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: How do I handle invalid controllers (/showthread.php?tid=18540) |
How do I handle invalid controllers - El Forum - 05-10-2009 [eluser]The Mask[/eluser] Hi, What is the best way to handle invalid controllers? For example, if somebody keys the URL as www.mysite.com/xyz where a controller does not exist called xyz. It would be cool to simply redirect this to the default controller without the user seeing any error message. Thanks How do I handle invalid controllers - El Forum - 05-10-2009 [eluser]Dam1an[/eluser] There are 2 possible ways to di this that come to mind 1. Extending the Exception class, and instead of displaying the 404 page, redirect to your controller 2. Add a catch all route at the end whih points to your controller (You would need to define all your valid controllers before it though, so this can get a bit messy) How do I handle invalid controllers - El Forum - 05-10-2009 [eluser]The Mask[/eluser] Thanks for the quick reply. I can't see any reference to an Exception class in the help though. Any ideas? How do I handle invalid controllers - El Forum - 05-10-2009 [eluser]Dam1an[/eluser] The exception class is documents under errors It can be found in Code: /system/libraries/Exceptions.php How do I handle invalid controllers - El Forum - 05-11-2009 [eluser]Michael Wales[/eluser] ew.. no no no - you should show a 404! The user is requesting a resource that doesn't exist, that is the definition of a 404. By not throwing a 404 and instead redirecting to a controller you will get massive reductions in pagerank for duplicate content. Essentially, any of your competitors could create a page that links to 100 or so random, invalid controllers on your site, Google/Yahoo/MSN now see that 100 of your pages are exactly the same and they deduce that your site is nothing but spam. You don't have to go with the default browser 404 design, you can design up the page however you want, add some helpful tips to the user (like links to what they may have been trying to access), etc. See this link for ideas: http://www.smashingmagazine.com/2009/01/29/404-error-pages-one-more-time/ How do I handle invalid controllers - El Forum - 05-11-2009 [eluser]sl3dg3hamm3r[/eluser] [quote author="Michael Wales" date="1242061960"]By not throwing a 404 and instead redirecting to a controller you will get massive reductions in pagerank for duplicate content. Essentially, any of your competitors could create a page that links to 100 or so random, invalid controllers on your site, Google/Yahoo/MSN now see that 100 of your pages are exactly the same and they deduce that your site is nothing but spam.[/quote] I never thought of that point. But I guess it is okay to set the header to a 404 (like in the default CI-errorpage), and show a nicely formatted page like 'oops, the called site doesn't exist etc.', by loading any view? How do I handle invalid controllers - El Forum - 05-11-2009 [eluser]Michael Wales[/eluser] Yeah - as long as you get that 404 in there. How do I handle invalid controllers - El Forum - 05-11-2009 [eluser]sl3dg3hamm3r[/eluser] errm... you mean besides the header I also need to have it as plain-text in the view? How do I handle invalid controllers - El Forum - 05-11-2009 [eluser]The Mask[/eluser] Thanks for the 404 info. Some interesting templates on the link too. Best regards How do I handle invalid controllers - El Forum - 05-11-2009 [eluser]Michael Wales[/eluser] Quote:errm… you mean besides the header I also need to have it as plain-text in the view?No - just the header() call is all that is required. |