Forum Moderators: coopster

Message Too Old, No Replies

Getting server to use 404 instead of 302

... from an .htaccess mod_rewrite

         

internetheaven

10:26 pm on Aug 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use a mod_rewrite in .htaccess like this:

RewriteEngine On
RewriteRule products\/(.+)\.html$ /folder/product.php?productnumber=$1 [NC,L]

but when we remove products from our database and someone goes to a page that is no longer there, e.g. the product that used to be at:

http://www.example.com/folder/products/12345.html

the server hearder comes back as:

Status: HTTP/1.1 302 Found

even though the .htaccess file has:

ErrorDocument 404 http://www.example.com/404.html

Of course, this is causing all sorts of confision in our search engine listings as instead of getting a 404 error when trying to retrieve a product page, they are getting a 302. Any suggestions?

Longhaired Genius

10:48 pm on Aug 27, 2005 (gmt 0)

10+ Year Member



If I understand you correctly, a page is found (created by the server) - there's just nothing on it.

You can take care of it in the script:

Before any output to the browser check some condition that would mean a succesful page then have php code like this:

$errorDocument404 = 'http://www.example.com/404.html';
if (!$someCondition) {
header("HTTP/1.0 404 Not Found");
include $errorDocument404;
exit;
}

jd01

1:18 am on Aug 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ErrorDocument 404 http://www.example.com/404.html

...is the problem.

The location of an error document should be a server-relative path
EG ErrorDocument 404 /404.html.

By default, a full URL will *correctly* generate a 302 (not-defined) redirect in either the .htaccess or httpd.conf, unless otherwise defined (307 temporary - 301 permanent).

The overly simple explanation is http:// generates an *external* request to the server - redirect.

A server relative path generates an *internal* request on the current serve - rewrite.

(Do not take the preceding as a full literal reason, just an example of the default handling differences between internal and external requests and the difference between rewrite and redirect.)

Justin

internetheaven

12:29 pm on Aug 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I understand you correctly, a page is found (created by the server) - there's just nothing on it.

No, that only occurs if you use the actual URL:

http://www.example.com/folder/product.php?productnumber=24065494

the mod_rewrite URL for /folder/ that brings up the information at the above URL is:

http://www.example.com/folder/products/24065494.html

but when that product no longer exists it uses the ErrorDocument 404 which is also in the same .htaccess file to direct the user to http://www.example.com/404.html, only the server header returns it as a 302 redirect, not a 404. (As checked on [searchengineworld.com...]

The location of an error document should be a server-relative path EG ErrorDocument 404 /404.html.

Thanks, that is now returning 404s for errors on the root directory, but still 302 for files in the /folder/ even though I have put ErrorDocument 404 /404.html in both of them. The strange thing is, that if a user goes to:

http://www.example.com/folder/products/24065494.html

which product no longer exists, the browser displays the URL:

http://www.example.com/folder/products/

whilst showing the information from the /404.html file.

internetheaven

12:31 pm on Aug 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, now here's a crazy thing, I changed the .htaccess file to read:

ErrorDocument 404 ../404.html

and if you go to any no longer existent product you get the standard Internet Explorer 404 file (i.e. "The page cannot be found The page you are looking for might have been removed, had its name changed, or is temporarily unavailable ...")

but [searchengineworld.com...] is still returning a 302?

jd01

6:28 pm on Aug 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is the .. a typo?

ErrorDocument 404 ../404.html

If not, the correct format is:
ErrorDocument 404 /404.html

Justin

Added: Sorry to be short, it was correct in the first post... will come back to it in a while.

[edited by: jd01 at 6:45 pm (utc) on Aug. 28, 2005]

ergophobe

6:43 pm on Aug 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



IE is returning it's standard because your custom page is too small. IE will only show it if it is larger than a certain (fairly small) size, but I forget what it is.

The thing I don't get is why you are getting a 404 at all, regardless of whether you use a relative path or not. With your rewrite, by default you should be rewriting

this:
http://www.example.com/folder/products/24065494.html

to this:
http://www.example.com/folder/product.php?productnumber=24065494

Whether product 24065494 exists or not should be irrelevant in terms of the server return code. You should not get a 404 unless product.php does not exist. As LG said, you should have some logic in the product.php script that sends the 404 and redirects to the error page from within the script.

internetheaven

10:16 pm on Sep 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, it seems as thought the mod_rewrite is to blame like you said. If you use:

http://www.example.com/folder/products/24065494

you get a 404. You only get the 302 when the .html extension is used:

http://www.example.com/folder/products/24065494.html

as it is part of the re-write. Thanks for the coding Longhaired Genius, I slapped it in at the top of the script but it's still giving out 302s. I've tried it with:

$errorDocument404 = 'http://www.example.com/404.html';
if (!mysql_num_rows($res)) {
header("HTTP/1.0 404 Not Found");
include $errorDocument404;
exit;
}

and $errorDocument404 = '/404.html'; and I've removed the ErrorDocument 404 /404.html from the /folder/.htaccess file.