Page 1 of 1

404 redirect trick

Posted: Tue Jul 21, 2009 1:31 pm
by s.dot
Hey guys,

A client site of mine is using the 404 redirect trick to handle virtual pages. eg domain.com/mypage.html doesn't exist.. so it goes to the 404 script and queries the database for mypage.html and displays the info and template.

Is there any way to prevent the server from logging a 404 error when this happens? The reason I ask is because the log file gets too big too fast and eventually crashes the server.

I know it could be done with mod_rewrite, but as far as I know the pages would have to start with something common to be able to detect them.. like..

Code: Select all

rewriteEngine On
rewriteRule ^something/(.+).html$ /script.php?page=$1
However the pages don't start with anything common so perhaps some really tricky mod rewrite?
What about PHP header() ?

Is this avoidable at all (without changing the 404 redirect trick setup)?

Re: 404 redirect trick

Posted: Tue Jul 21, 2009 1:43 pm
by Christopher
Why not use mod_rewrite to go to a Front Controller that does a file_exists() check for what was requested. You can do complex lookups and logic inside the FC. And you do get the 404s.

Re: 404 redirect trick

Posted: Tue Jul 21, 2009 3:28 pm
by inghamn
You'd want to rewrite anything that's not a file or directory on the hard drive. You would route it to your own PHP script that would do the lookup. That PHP script would only throw an actual 404 if it really couldn't find what they asked for.

Code: Select all

 
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !\.
    RewriteRule .? /404.php [L]
 

Re: 404 redirect trick

Posted: Wed Jul 22, 2009 1:56 am
by allspiritseve
Also, keep in mind that if you use inghamn's suggestion (which you should) you then have the burden of throwing 404 errors from your php script, if a file truly doesn't exist and you don't have a 'virtual page' for it.

Re: 404 redirect trick

Posted: Wed Jul 22, 2009 3:51 am
by Benjamin
That site isn't sending 404 headers to search engines is it?