Forum Moderators: phranque

Message Too Old, No Replies

Apache 404 Redirect

Apache 404 Redirect

         

0x416C616E

9:10 am on Feb 14, 2004 (gmt 0)

10+ Year Member



Hello,
First off, if this topic is posted elsewhere on the site, sorry. I searched through 9 similar topics, but none answered my question.

I read that a "fix" to the problem that some search engines have with a dynamic page could be solved with a 404 error redirect. For example, instead of somepage.php?category=cars&name=mustang you could link to a nonexistant page of /cars/mustang/somepage.php. When the server doesn't find that page, it redirects to the page set in .htaccess, and you can explode the referring URL.

The tutorials I read said to use

$url = explode("/",$REQUEST_URI);

but this wouldn't work because $REQUEST_URI is delivering the 404.php and any variables after the? (which are what I want to avoid). So I tried
$url = explode("/",$_SERVER['HTTP_REFERER']);

and its not picking up the referer at all.

Is this because of a variable set in httpd.conf. Or do the server variables for my browser get reset when apache redirects through .htaccess, or maybe something else?

Thanks

jdMorgan

6:56 pm on Feb 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



0x416C616E/Alan,

Welcome to WebmasterWorld [webmasterworld.com]!

You could try REQUEST_FILENAME to get the filename without query string, etc.

I would strongly advise against using this "404 redirect" method for two reasons. First, the server will return a 404-Not Found response to the requesting user-agent, which will confuse some search engine spiders. Secondly, even if it does not confuse them, it still requires a "handshake" with the user-agent. That is, the user-agent must receive the 404 response, and then re-request the resource from the URL given in the 404 response message-body.

A good policy is to never push your luck by returning inappropriate response codes, and to never do any external redirect when an internal rewrite will do. The first leads to unexpected/unwelcome results, and the second is horribly inefficient.

You will be a lot happier with the results if you simply use mod_rewrite to internally rewrite these requests to your script.

HTH,
Jim

0x416C616E

7:05 pm on Feb 15, 2004 (gmt 0)

10+ Year Member



Thanks alot Jim,

I looked into mod_rewrite, and its exactly what I need.

Alan