-2

I am trying to overwrite HTTP Header response code to another response code using .htaccess.

I want to change

HTTP 404 to 200 OK

HTTP 403 to 200 OK

.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>

Using PHP it easy : header("Status: 200 OK");

But i want to do this using .htaccess. Any idea how can we overwrite HTTP response code

Note : i am not looking for redirect.

15
  • 1
    @MrWhite because my team-leader and client demanding it :(. Can you help me with solution how to overwrite HTTP response code with htaccess? Commented Jan 2, 2022 at 11:47
  • 1
    @MrWhite my clients want overwrite of response code with htaccess. Can you help me with htaccess code which overwrite the response code :) Commented Jan 2, 2022 at 13:36
  • 1
    As I said, it depends how the response code is being set... if the 404/403 response is being set by the application (eg. PHP) then you can't simply overwrite the response code using .htaccess. So, as it stands, your question is incomplete... How is the 404/403 response being set in the first place? Commented Jan 2, 2022 at 14:45
  • 1
    "if someone access a link which do not exist" - And that link would ordinarily map to a static file? Not through a CMS like WordPress etc.? And you still want to return the "Not Found" page but with a 200 OK response? Commented Jan 2, 2022 at 16:28
  • 2
    @shanmugapradeep You've asked exactly the same question twice! I have marked the more recent question as a duplicate of this question, which is not closed. Commented Jan 8, 2022 at 17:29

1 Answer 1

3

This was really a very interesting problem, took me few hours to go through docs and test many times to come up with following solution.

Add this snippet in your document root .htaccess:

# serve /index.php for 200
ErrorDocument 200 /index.php

# serve FallbackResource with status=200 to handle 404
FallbackResource /index.php

# declare ErrorDocument for all the error codes you want to handle
# note that dummy file or directory doesn't exist.
# This will set REDIRECT_STATUS to 403
ErrorDocument 403 /dummy

RewriteEngine On

# force Status=200 if REDIRECT_STATUS is 403
RewriteCond %{ENV:REDIRECT_STATUS} =403
RewriteRule ^ - [L,R=200]
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for sharing nice solution sir, so this means if a redirect status is 403 OR 404 then it will make them 200 OK? Sorry may be I didn't get it, could you please explain more on same, thank you.
Yes that's right @RavinderSingh13. REDIRECT_STATUS is set due to use of ErrorDocument directive.
Thanks for clarifying it sir but then a simple question is: why for non-existing pages(404 error) someone needs a 200(without serving any actual file from backend)? Though I read the comments but still would like to know your views on same.
Personally I would just place a custom 404 page for viewers but OP had different requirements
Yeah I totally agree with you, because if an admin sees logs(in case of an issue, specially localhost logs of tomcat etc) then 200 status in logs may confuse them(if they don't know about rules existence), though still difficult to say without knowing full requirement but as you mentioned making custom 404 page will be great, thank you sir.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.