daxu
daxu

Reputation: 4084

can CodeIgniter display 404 page for mistyped index.php url?

I am using CodeIgniter on apache php, If I mistype url on controller level or method level (e.g. http://www.example.com/index.php/mistypedcontroller/mistypedfunction) I get the 404 page.

But if I type in http://www.example.com/mistyped.php/controller/function, I got a blank page. Is there a way to show the same 404 page here?

Upvotes: 0

Views: 255

Answers (1)

Sparky
Sparky

Reputation: 98738

"can CodeIgniter display 404 page for mistyped index.php url?"

No, it cannot. If you mistype index.php, then you are no longer within/using CodeIgniter.

Look inside this file and you'll see that index.php is constructing everything, and the very last line is including the CodeIgniter.php core file.


www.example.com/index.php/mistypedcontroller/mistypedfunction

This first one above is using index.php, so it's generated by CodeIgniter.

www.example.com/mistyped.php/controller/function

This second one, since it's not using index.php, has absolutely nothing to do with CodeIgniter.

Is there a way to show the same 404 page here?

It would have to be a standalone file and configured for your particular server via Apache's .htaccess file using the ErrorDocument 404 option.

Alternatively, if you simply follow the documentation for removing the index.php from the URL, your situation above could never occur; your broken URL example will cause a CodeIgniter 404 error because any/every URL (pointing to a file that doesn't exist) is forced into your index.php file.

If your Apache server has mod_rewrite enabled, you can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the “negative” method in which everything is redirected except the specified items:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

In the above example, any HTTP request other than those for existing directories and existing files is treated as a request for your index.php file.

Upvotes: 3

Related Questions