Forum Moderators: phranque

Message Too Old, No Replies

404 script without the 404 header

Wanting www. .com/anything urls, without 404's

         

eaden

11:06 pm on Mar 13, 2003 (gmt 0)

10+ Year Member



I want to enable urls such as
[www....] .com/joe
[www....] .com/jack
etc

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.

jdMorgan

1:08 am on Mar 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



eaden,


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

eaden

1:48 am on Mar 14, 2003 (gmt 0)

10+ Year Member



Thanks for the reply,

The problem with that is that it performs an external redirect, changing the URL in the browser. I would like the url in the browser to stay as www. .com/joe and not /404handler.php

jdMorgan

1:59 am on Mar 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



eaden,

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

HTH,
Jim

[edited by: jdMorgan at 2:38 am (utc) on Mar. 14, 2003]

eaden

2:10 am on Mar 14, 2003 (gmt 0)

10+ Year Member



Hi there,
So I guess this code will do what I want it to do? ( in my .htaccess )

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

jdMorgan

2:45 am on Mar 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



eaden,

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

mycal

2:48 am on Mar 18, 2003 (gmt 0)



I'm doing this on my website <snip>
There are no documents here except index.php, though
any url you type is generated by the index.php.

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]

eaden

5:24 am on Mar 19, 2003 (gmt 0)

10+ Year Member



Thanks mycal, that is the one, Works perfectly :)

There must be something funny with this forum software as it always seems to remove the space after } before the!-f