views:

148

answers:

3

Hi friends,

How can I redirect such link (below) to 404 error page?

http://www.blabla.com/category/sub/.html

I generate a SEO friendly links, normally products has url like blabla.com/category/sub/product.html

the page doesnt give any error. it just display blank content. I mean i can see static texts like CATEGORY NAME header and but there is no dynamic categoryname next to that, because of url is wrong and cant get record from db. page doesnt display dynamic contents, but display static contents

Appreciate helps!! thanks

A: 

You should use .htaccess to catch the error:

ErrorDocument 400 /errors/400.html
ErrorDocument 401 /errors/401.html
ErrorDocument 403 /errors/403.html
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html

Apache will know now that it needs to load one of these pages depending on what error occured. You can instead us a php script (e.g. 404.php) for a dynamic solution.

That means http://www.blabla.com/category/sub/.html will display an error message, and the server will generate an error header (404 in this case) so search engines won't crawl it.

You use this code by creating a file in your http root directory called .htaccess and plasting the code inside. .htaccess is not a file extention, but a file itself.

More information can be found here

ILMV
the page doesnt give any error. it just display blank content. I mean i can see CATEGORY NAME header and but there is no dynamic categoryname because of url is wrng. page doesnt display dynamic contents, but display static contents
artmania
Right well you didn't specify that when I typed my post, I think you need to look at Lucas Renan's answer.
ILMV
yes, sorry my mistake :S
artmania
A: 

if the query don't result any row, you can set the header for 404 - not found and redirect to a 404 page

for example:

<?php    

    //$rows is the num_rows of the query
    if ($rows > 0) {    
      //your code here    
    } else {    
      header("HTTP/1.1 404 Not Found"); 
      header("Location: 404.html");
      exit();
    }

?>
Lucas Renan
hi, yes it fix my problem : ) but i thought there should be more efficient way to make it via htaccess or sth... rather than php controls
artmania
i made it with returned id value control : ) same way as you suggested. thanks again
artmania
Don’t send a 404 and do a redirect on the same time! The redirect will cause a 302 instead.
Gumbo
you can include the page "404.php" (for the redirect problem)
Lucas Renan
A: 

Do not redirect! A redirect (30x) is not the same as sending a 404! Just send the proper status code along with your error document:

if (!$documentFound) {
    header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
    readfile('error404.html');
    exit;
}
Gumbo