404 redirect trick

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

404 redirect trick

Post 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)?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: 404 redirect trick

Post 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.
(#10850)
User avatar
inghamn
Forum Contributor
Posts: 174
Joined: Mon Apr 16, 2007 10:33 am
Location: Bloomington, IN, USA

Re: 404 redirect trick

Post 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]
 
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: 404 redirect trick

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: 404 redirect trick

Post by Benjamin »

That site isn't sending 404 headers to search engines is it?
Post Reply