简体   繁体   English

/cat/sub/.html的htaccess 404错误重定向(如url)

[英]htaccess 404 error redirection for /cat/sub/.html like urls

How can I redirect such link (below) to 404 error page? 如何将此类链接(如下)重定向到404错误页面?

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

I generate a SEO friendly links, normally products has url like blabla.com/category/sub/product.html 我生成一个SEO友好链接,通常产品的网址如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. 我的意思是我可以看到诸如CATEGORY NAME标头的静态文本,但由于url错误并且无法从db获取记录,因此旁边没有动态categoryname。 page doesnt display dynamic contents, but display static contents 页面不显示动态内容,但显示静态内容

Appreciate helps!! 感谢帮助!! thanks 谢谢

if the query don't result any row, you can set the header for 404 - not found and redirect to a 404 page 如果查询未产生任何行,则可以为404设置标头-找不到并重定向到404页面

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();
    }

?>

You should use .htaccess to catch the error: 您应该使用.htaccess来捕获错误:

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. 现在,Apache将知道根据发生的错误需要加载这些页面之一。 You can instead us a php script (eg 404.php) for a dynamic solution. 您可以改为使用我们的PHP脚本(例如404.php)提供动态解决方案。

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. 这意味着http://www.blabla.com/category/sub/.html将显示错误消息,并且服务器将生成错误标头(在这种情况下为404),因此搜索引擎不会抓取它。

You use this code by creating a file in your http root directory called .htaccess and plasting the code inside. 您可以通过在http根目录中创建一个名为.htaccess文件并将代码粘贴到内部来使用此代码。 .htaccess is not a file extention, but a file itself. .htaccess不是文件扩展名,而是文件本身。

More information can be found here 更多信息可以在这里找到

Do not redirect! 不要重定向! A redirect (30x) is not the same as sending a 404! 重定向(30x)与发送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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM