简体   繁体   中英

How to return a 404 status code for a content page?

I am using SiteChecker.Pro to check my website for seo errors and it tells me this: 4xx client errors The explanation is that when a visitor hit a non-existent page, my scripts site-wide are doing a JS redirect to a custom 404 page (which in fact has 200 status code). What is the best strategy to return a custom page with a content but with 404 status code in the header?

Why Sitechecker reported this as an issue. In terms of SEO it is important to avoid a situation where same content is returned to search bots under different URLs. This is to avoid duplication of content, as users don't want to see same pages in the search results. Returning a 404 status code for non-existing pages tells bots that the content in the page should not be crawled or indexed. If you, however, return a 200 status code in server headers, your content can be indexed and then regarded as duplicate.

You can use PHP to give a 404 error using:

<?php
http_response_code(404);
?>

(See: PHP http_response_code() Function )

Some hosting options will also work with a 404.html file in the root, but not always.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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