I am using mod_rewrite in my PHP framework to redirect user-friendly links to one main entry point of the application. The .htaccess file I am using looks like that:
<IfModule mod_rewrite.c>
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
Now, I would like to use that rule with the ErrorDocument directive. How should I do this? My framework sets the response code to 404 when there’s a controller missing:
/**
* Sets the HTTP response code.
*
* @param int $errorCode The response code to return. <b>Supported codes: </b>301, 307, 400, 401, 403, 404, 500,
* 501, 502, 503
*/
function setHTTPStatus($errorCode) {
$httpStatusCodes = array(301 => 'Moved Permanently', 307 => 'Temporary Redirect', 400 => 'Bad Request',
401 => 'Unauthorized', 403 => 'Forbidden', 404 => 'Not Found', 500 => 'Internal Server Error',
501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable');
if (array_key_exists($errorCode, $httpStatusCodes)) {
header("HTTP/1.0 $errorCode $httpStatusCodes[$errorCode]", true, $errorCode);
exit;
}
}
I tried adding the line ErrorDocument 404 /index.php?url=404 and ErrorDocument 404 /error but none seem to have worked and I get the default browser’s error page.
What is wrong?
Thank you.
There is no easy way to do this via the .htaccess file. Once you’re in php code land, apache has processed through the .htaccess file and httpd.conf files and made a handoff to the frameworks front
controller–in this case the index.php file for the framework.
What framework are you using?
The best way to handle this is to use the frameworks error handling, not the baked-in apache handling. The apache 404 is fine for any non-framework served pages like an image, css, js, etc… Your framework should have a configuration file somewhere to set the default error view.