Forum Moderators: coopster
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?
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;
}
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
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.
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?
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.
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.