0

i am trying to change HTTP STATUS of 404 and 403 to 200 OK

With PHP, it easy to do but the problem is my website is .html and i cannot change it to .php for some reason.

Code (PHP) :

header("Status: 200 OK");

i want same on .htaccess. so it will automatically change HTTP response code to 200 Ok for 404 & 403.

Htaccess Code :

RewriteEngine On

#ErrorDocument
ErrorDocument 403 /forbidden.html
ErrorDocument 404 /notfound.html
ErrorDocument 500 /servererror.html

# Disable directory browsing 
Options -Indexes

#Change status code  to 200 OK
<IfModule mod_headers.c>
  <If "%{REQUEST_STATUS} in { '404','403', '500' }">
      Header set HTTP/1.1 "200 OK"
      Header set HTTP/2 "200 OK"
  </If>
</IfModule>
Stellan Coder
  • 323
  • 1
  • 11
  • How is 404 setup in your .htaccess as of now? – nice_dev Jan 08 '22 at 08:18
  • @nice_dev `ErrorDocument 404 /notfound.html` like this – Stellan Coder Jan 08 '22 at 08:19
  • 1
    So, if some URL isn't found, you return `notfound.html`. What could be the reason to send 200 status for a document that isn't found on server apart from 404? – nice_dev Jan 08 '22 at 08:33
  • @nice_dev yes right, if content or URL do not exists then it return `notfound.html` with `404` response code and all i want it to overwrite `404` response code to `200 OK` – Stellan Coder Jan 08 '22 at 08:35
  • @nice_dev all i want, if any content/link to do not exist, it will return `notfound.html` with `200 OK` response instead of `404`. I want this because i am trying some dynamic content for error pages – Stellan Coder Jan 08 '22 at 08:37
  • By _dynamic content for error pages_ you mean custom error messages? – nice_dev Jan 08 '22 at 08:38
  • @nice_dev it not a custom message. It a different content like articles, deals etc. Can you help me how can i overwrite `404` to `200 OK` – Stellan Coder Jan 08 '22 at 08:40
  • I will test on some of my servers and get back to you. Apparently, `REQUEST_STATUS` isn't supported for the `If` block. So you can remove it. – nice_dev Jan 08 '22 at 09:58
  • 1
    @nice_dev got it. I will wait for your reply. Thanks for helping :) – Stellan Coder Jan 08 '22 at 10:00

1 Answers1

3

This was relatively simpler. Instead of local path to the document, you can mention full URL since the request gets redirected to this page to return a 200 ok status code . So, your .htaccess would look like

RewriteEngine On

#ErrorDocument
ErrorDocument 403 https://your_site.com/forbidden.html
ErrorDocument 404 https://your_site.com/notfound.html
ErrorDocument 500 /servererror.html

# Disable directory browsing 
Options -Indexes
nice_dev
  • 17,053
  • 2
  • 21
  • 35
  • With this code, if someone visit `example.com/anything-not-in-website` then it will show the content of `deals_page.html` page with `200 OK` response instead of `404` - Right? – Stellan Coder Jan 08 '22 at 08:48
  • How can i do this same with `403` response code to `200 OK`? – Stellan Coder Jan 08 '22 at 08:49
  • @shanmugapradeep Yes, it would for 404, you can test it with your site. For 403 it should not be done as it is a permission issue. I would rather suggest you to show appropriate error message not confuse the user using your system. – nice_dev Jan 08 '22 at 08:50
  • I am not confusing the user. example, i am trying to display message like `Oops, you landed on wrong but would you like to check our recent new year offer` and same for 403 like something `Access to this service is not allowed but you love to access our new year limited deal`. All i need the response code to show `200 OK` so the bot/crawler claw those pages. – Stellan Coder Jan 08 '22 at 08:53
  • BOT/crawlers always ignore the pages with `404` & `403` response code. this the reason i am trying to overwrite it with `200 OK` – Stellan Coder Jan 08 '22 at 08:54
  • @shanmugapradeep Ok, do you have separate .html files for `403`? – nice_dev Jan 08 '22 at 09:01
  • Yes, i do have. `notfound.html` for `404` and `forbidden.html` for `403` – Stellan Coder Jan 08 '22 at 09:04
  • @shanmugapradeep You can read this [`blog`](https://www.searchenginejournal.com/404-errors-google-crawling-indexing-ranking/261541/) if you are so concerned about SEO rankings. – nice_dev Jan 08 '22 at 09:04
  • I will check that article but it not just about SEO. There is many crawlers like listing, advertising etc, those crawlers ignore any pages with `404` or `403` response code. The reason i am just trying to change response code to `200 OK` – Stellan Coder Jan 08 '22 at 09:07
  • @shanmugapradeep Is it safe to say that you wish to return `200` ok response no matter what..? – nice_dev Jan 08 '22 at 09:07
  • Yes right, i want `200 OK` response code for `404` and `403` – Stellan Coder Jan 08 '22 at 09:12
  • @shanmugapradeep I updated my answer. Can you test it now? – nice_dev Jan 08 '22 at 09:21
  • @shanmugapradeep I made it more specific with `404` and `403` now in my answer. Can you test again at your end? – nice_dev Jan 08 '22 at 09:27
  • Not working. it still show `403 error` and it ignoring `error document 403` – Stellan Coder Jan 08 '22 at 09:42
  • @shanmugapradeep Share your .htaccess code in the question by editing it. Also, please don't tick any answer unless it works for you. I hope you know how it is done on SO here. – nice_dev Jan 08 '22 at 09:43
  • I have updated the code in question and i have added `HTTP/2` because in firefox it show it `version HTTP/2` and i made that tick for the previous code only for `404` – Stellan Coder Jan 08 '22 at 09:49
  • @shanmugapradeep I presume you could simply do `ErrorDocument 200 /forbidden.html ErrorDocument 200 /notfound.html`. – nice_dev Jan 08 '22 at 09:51
  • Then it will redirect all `200 OK` page to `notfound.html` instead of 404 page – Stellan Coder Jan 08 '22 at 09:53
  • @shanmugapradeep I have updated my answer. Hope it helps this time. – nice_dev Jan 08 '22 at 13:24
  • It works! but it not what i am looking for. it redirect to the page. if someone enter `example.com/not-in-website` then it will redirect to `example.com/notfound.html` using `ErrorDocument 404 https://your_site.com/notfound.html` but i only want contents not a redirect. if someone visit `example.com/not-in-website` then it should show the content of `notfound.html` on same URL without redirecting and response code should be `200 OK` response code and that why i am using `ErrorDocument 404 /notfound.html` – Stellan Coder Jan 08 '22 at 15:27
  • @shanmugapradeep I am afraid that's the best I can go for looking at the limited options you have. – nice_dev Jan 08 '22 at 16:17
  • 1
    No problem mate. you tried best and i am thankful for your valuable time and helping me to find solution. – Stellan Coder Jan 08 '22 at 17:05
  • 1
    @shanmugapradeep "Access to this service is not allowed" - How is this "403" being triggered in the first place if this is seemingly a static `.html` site? (Although I asked this on your [earlier (dupe) question](https://stackoverflow.com/questions/70554192/overwrite-http-response-code-using-htaccess) and you were unable to provide an answer.) The point is, if this is triggered by your application (after all, what else would be triggering this?) then you cannot override this with standard modules in Apache config/`.htaccess`. – MrWhite Jan 08 '22 at 17:13