So, I am playing with urls a bit with a PHP app I am building.
I am using mod_rewrite using .htaccess. Here is what that looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1
</IfModule>
I thought if a URL was written like below:
http://localdomain.com/controller
I would expect ‘controller’ ending without issues as part of the $_SERVER[‘PATH_INFO’]. However, on Ubuntu 12.04 running Apache 2.2.22 I get a 404 Not Found error instead:
NOT FOUND
The requested URL /index.php/controller was not found on this server.
It appears that ‘controller’ is being properly addressed after a slash following index.php, but Does anyone have any idea why I am getting a 404 instead of the expected response?
Ultimately I am trying to capture the ‘controller’ to use in an MVC framework I am building.
In this case I have a very simple index.php with ONLY the following:
<?php
echo $_SERVER['PATH_INFO'];
Yet, I still get a 404 error instead of the expected path info.
NOTE: Going to just http://localdomain.com/ or http://localdomain.com/index.php work perfectly. mod_rewrite is on and working, and AllowOverride is set to All.
If you’re running apache 2.2.22, then your PHP code is now responsible for specifying the 200 response if the controller function was found.
Apache 2.2.22 seems to take a different philosophy on rewrites compared to 2.2.14. Long story, but it makes sense. It assumes that if you’re rewriting to a front-controller like CodeIgniter does, then the PHP code is responsible for deciding whether the requested URI was found or not. Apache waits for the PHP code to send header(‘HTTP/1.1 200 OK’), and if it doesn’t then it sends header(‘HTTP/1.1 404 Not Found’).
Interestingly, the actual content coming from your controller is probably sent in the response body too. But many HTTP clients ignore that if the response code was 404 (like MSIE, wget, etc.)
The short version is that apache 2.2.14 (on platinum) is optimistic, assuming 200 unless otherwise specified by PHP, whereas apache 2.2.22 is pesimistic, assuming 404 unless otherwise specified by PHP.
So somewhere in your code, you must place:
This should be placed just after the front-controller has determined that the URI is valid and a class and method exists and can be called for this URI, but before the class and method are actually called.
In CodeIgniter 2.1.3, it looks like this: