I'm developing a highly dynamic website that needs to handle a lot of URI that do not actually have a file behind them. So I used .htaccess to redirect all non-existing URI to a php file that displays what I need based on the URI by looking into various tables in a database and including the correct web-pages.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /urihandling.php [L,NC,QSA]
This will produce a 200 OK Status and keeps the URI in the address-bar so it's exactly what I want.
Now my problem is, if the user accesses a URL that is not in the database, I want to redirect to a custom 404 page (e.g. 404.html) and produce the status code 404 Not found.
First I tried:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /urihandling.php [L,NC,QSA]
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
That does not load 404.html when I use
header("HTTP/1.0 404 Not Found");
in my urihandling.php file. In fact, it doesn't load anything, it just shows a blank page (if I erase the Rewrite commands, the 404 works fine, so the syntax etc. seems to be ok). I thought maybe the problem is that the 404 triggers the rewrite and then it's going into some sort of redirect-loop or something.
Then I tried this:
RewriteEngine On
RewriteBase /
RewriteRule /404.* /404.html [R=404,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /urihandling.php [L,NC,QSA]
ErrorDocument 404 /403.html
ErrorDocument 403 /403.html
With or without the ErrorDocument makes no difference. Then in my urihandling.php I used
header('Location: http://somesite.com/404.html');
This works as far as the user is concerned, but looking at the status codes in the HTTP headers, I get a HTTP/1.1 302 Moved Temporarily
So my question is, how can I achive this and still get a 404 status code?
Thanks for the help!