Forum Moderators: phranque
I am using apache. As the site is database driven, I want to just be able to add them to the database without making files called "jack" or without adding directives for all urls. So, I'm looking for a way to run a script on 404, but without giving the 404 error.
I tried ErrorDocument 404 /myscript.php but that gives a 404 header.
ErrorDocument 404 http://www.yourdomain.com/myscript.php
(including method and canonical URL) should give a 302-Moved Temporarily instead of a 404, if you'd like to try that. In most cases, this behaviour is the cause of problems for webmasters who want a 404 response... Interesting to see that you actually have a use for it!
Jim
Well, why didn't you say so? :)
Here's some mod_rewrite code I wrote for a different but similar problem in this thread [webmasterworld.com], which you can adapt to handle your missing URLs with a transparent redirect.
# Redirect missing catalog images to a default image. Let other missing resource requests 404 normally.
RewriteCond %{REQUEST_URI} !-U # if requested resource doesn't exist
RewriteRule ^catalog_image_path/.*\.gif$ /default_image_path/default.gif [L] # redirect to default image
[edited by: jdMorgan at 2:38 am (utc) on Mar. 14, 2003]
RewriteEngine on
RewriteCond %{REQUEST_URI}!-U #
RewriteRule ^.*$ /404.php [L] #
Using thit code, i get a 500 error, and this in my error log:
[Fri Mar 14 15:08:17 2003] [alert] [client xxx.xxx.xxx.xxx] /var/www/users/xxxxx.com/htdocs/.htaccess: RewriteCond: bad flag delimiters
Sorry, it won't work with the in-line comments I included. Put them on their own lines, or just delete them. Also, a space is required in front of !-U.
If possible, make the RewriteRule more selective. For example, you may not need to redirect missing images to your script. Or maybe you do. But this code uses an internal subrequest to check if the resource exists, so it will impact server performance. Anything you can do to minimize the number of internal subrequests will be good. Oh, in order for this caveat to make sense, remember that RewriteConds are only processed if the pattern of the RewriteRule matches... Counterintuitive, but true.
For easy access to reference links, see this Introduction to mod_rewrite [webmasterworld.com].
Jim
Here is my modrewrite rule:
RewriteEngine on
# if requested resource doesn't exist
RewriteCond /%{REQUEST_FILENAME}!-f
# redirect to default image
RewriteRule ^(.+) /
Hope this helps
mycal
[edited by: Woz at 3:21 am (utc) on Mar. 18, 2003]
[edit reason] no self promo URLs please [/edit]