Catch-all controller / route

If I want to catch every non-existent URL that was submitted to my web application and provide them with a 404 maintenance-free view, how would I do it?

Essentially, I need to record statistics based on these hits, but I need to do this by showing content, not 404 error.

As far as I can tell from application/config/routes.php , I could use

 $route['default_controller'] = 'catchall'; 

but I need this for my actual web application.

I could also use

 $route['404_override'] = 'catchall'; 

but I do not want to throw 404s.

I tried to use

 $route['(:any)'] = '$1'; 

but I need to write down the entire URL (for example, any segment length), and not just the first segment.

+7
source share
3 answers

Use $route['(:any)'] = 'catchall_controller' . Then in your controller, you can access the URI segments with $this->uri->segment(n) .

+6
source

Have you tried to add a few catch of all routes for different numbers of segments?

 $route['(:any)'] = '$1'; $route['(:any)/(:any)'] = '$1/$2'; $route['(:any)/(:any)/(:any)'] = '$1/$2/$3'; 

I suppose this will work, but there may be a more elegant way to do this.

+1
source

I don't know the difference between codeigniter and normal PHP, but in normal php you can edit htaccess or something else to define a 404 user page.

On user page 404, do something like this: $ Requested = $ _ SERVER ['REQUEST_URI'];

to get the url that was requested ... do your call ... and do it to clear 404:

heading

("HTTP / 1.1 200 OK"); title ("Status: 200 OK");

Then include your html or whatever you want to serve.

0
source

All Articles