is it possible to send 2 http status codes for 404 error page and redirect to homepage

459 Views Asked by At

in .htaccess file, is it possible to send 410 status code for 404 error page and then immediately redirect to homepage?

for example, when search engine crawl a 404 page, server send 410 status code and then redirect to homepage.

1

There are 1 best solutions below

0
On BEST ANSWER

No, it is not possible to send a 4xx and redirect (ie. 3xx status) in the same response - if that is the intention.

A 410 (or 404) response is one response. A 3xx HTTP redirect is another entirely different response. You do one or the other, not both.

Google will likely see 3xx redirects to the home page as soft-404s anyway (as they are generally a bad user experience).

However, you can send a 410 "Gone" status instead of a 404 "Not Found". Exactly how you do this will depend on your system. For example, if you have an existing 404 document then it may be easier to simply override the HTTP status when the response is sent to the client. To send a 410 when any response does not map to a physical file or directory then you can do something like the following using mod_rewrite in .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ - [G]