Forum Moderators: phranque
ErrorDocument 404 /404.php $id = 'foo';
header("HTTP/1.0 200 OK");
include "/home/example/www/whatever/index.php";
exit; RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI}/index\.php !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule .* 404.php [QSA,L] # now this breaks
RewriteRule ^this(.*) that/index.php?id=this [L]
# maybe because it keeps on reading and finds this?
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI}/index\.php !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule .* 404.php [QSA,L] they'll be redirected to 404.php via .htaccessPlease don't say this. Redirecting a 404 is incorrect, but what you’re doing is correctly defining an error document.
I'm guessing that maybe it is logged as soon as it hits the .htaccess?Exactly. I can remember it taking a long time to wrap my brain around the fact that the response the server sends out is not necessarily the response the user receives. You see this especially in a CMS, where all requests are 200 as far as the server is concerned.
I thought I would be slick and use this insteadBut, good grief, then you’re putting the server to all that extra work it has to do if you were running a CMS. If you do want to do this, you should put a RewriteRule at the very beginning of all your RewriteRules--in the same place you have the rule letting everyone see the 403 document--saying something like
RewriteRule ^404\.php - [L]
ErrorDocument 404 /404.php
Then, 404.php dissects the URL to find /foo, then checks a MySQL database to see if I have it in a table; if so, it includes the page that corresponds.
header("HTTP/1.0 200 OK");
RewriteRule ^this(.*) that/index.php?id=this [L] RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule .* /index.php [L] [edited by: phranque at 12:28 am (utc) on May 28, 2019]
[edit reason] unlinked urls [/edit]
this doesn't internally rewrite a potential 404 response to /404.phpActually, it does, because an error document is really just a special kind of rewrite. Your browser's address bar still shows the originally requested, blocked or nonexistent URL, while your screen displays the error document--which could perfectly well be a php page subject to all the usual php behaviors.
does the use of a custom error document show up in a rewrite log?No, because it doesn’t involve mod_rewrite. That’s why I said “a special kind of rewrite”: it’s functionally the same thing, although it’s done in a different way. Come to that, you could also think of a DirectoryIndex as a type of rewrite.