4

I don't want the apache of a website return any HTTP code 404,
the reason is that the accreditation council will scan all the website,
many javascript links and some incidents will cause the 404 code,
in fact, those links are valid but that checker will misjudge error.
So, i want to prevent the apache to return HTTP code 404,
How can I configure apache return HTTP cods 200 instead of 404 and show my define error page?

Andy
  • 407
  • 2
  • 14
  • 30
  • Why does Apache return a 404 when you say the link is valid? – CodeCaster Feb 26 '13 at 07:27
  • 1
    like this link, the checker will get a 404 code. javascript:parent.$.fancybox.open({href%20:%20'board.php?id=472',%20type:%20'ajax'}); – Andy Feb 26 '13 at 08:16
  • 4
    Then put that in the click event, not the href. Or fix the parser. – CodeCaster Feb 26 '13 at 08:29
  • "Can i configure that even though that file is not exist, but the apache always return HTTP Status code 200?" surely, but only a fool would do that search engines are stopping to index your site if your server act's that way for damned good reasons –  Jul 09 '13 at 14:25

3 Answers3

8

From: http://www.askapache.com/htaccess/apache-status-code-headers-errordocument.html

  1. Create a blank file in your main web directory named 404.. can be blank.
  2. Add this to your .htaccess file:

    Redirect 200 /404
    ErrorDocument 404 /404
    

That will change the Apache ErrorDocument to the /404 file. But the Redirect line causes requests for /404 to issue a 200 OK response instead of a 404.

If that doesn't work it is due to rewrites no doubt. So if that's the case add this under those first 2 lines but above any other rewrite lines in your .htaccess file:

Options FollowSymLinks ExecCGI

RewriteEngine On
RewriteBase /

# If the requested file doesnt exist
# and if the requested file is not an existing directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^4]* /404 [L,S=4000]

Explained in more detail at: http://www.askapache.com/htaccess/apache-status-code-headers-errordocument.html#Automate_ErrorDocument_Triggering

AskApache Htaccess
  • 1,110
  • 10
  • 9
  • 1
    Do you know how to do this, but instead of returning a page `/404`, return an actual response code? Like 404 or 401 – jlewkovich Jun 30 '16 at 23:58
  • I want to change the error code from 403 to 404 . Do you know how to do this? – Robert Ravikumar Apr 11 '19 at 05:34
  • I'd like to use this technique to redirect any requests for missing directories or files to index.html but simply changing `/404` to `/index.html` doesn't work. If I leave the `Redirect` out then `index.html` loads correctly but with a status `404` and I need status `200`. Any ideas? (I'm using static HTML pages, so PHP headers are not an option). – Fat Monk Jul 20 '21 at 16:27
  • 1
    I used your code but still it not working. see my question here : https://stackoverflow.com/questions/70554192/overwrite-http-response-code-using-htaccess – Stellan Coder Jan 02 '22 at 18:48
3

i think you can use this below simply code into PHP:

header("HTTP/1.1 200 OK");

you can test it.

DolDurma
  • 15,753
  • 51
  • 198
  • 377
  • 1
    Can i configure that even though that file is not exist, but the apache always return HTTP Status code 200? – Andy Feb 26 '13 at 07:29
  • This worked for me. I am using `ErrorDocument 404 /` which causes a 404 status. Manually setting a 200 response changed it from 404 to 200. – d-_-b Feb 23 '14 at 21:15
2

I use this:

<?php header("Status: 200 OK"); ?>

This code has to be put at the top of the .php file in the first line of code. Search engines, crawlers, browsers and anyone accessing the site will see the errordocument 404 page as a header 200.

Header 200 means the page exists. Google will crawl and index sites with header status 200.

Michael d
  • 305
  • 2
  • 16