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

anubhava
  • 761,203
  • 64
  • 569
  • 643
Stellan Coder
  • 323
  • 1
  • 11
  • This sounds like an X-Y problem... what is triggering the 404 or 403 in the first place? In your example you are explicitly triggering a 404. Why? – MrWhite Jan 02 '22 at 11:17
  • 1
    @MrWhite because my team-leader and client demanding it :(. Can you help me with solution how to overwrite HTTP response code with htaccess? – Stellan Coder Jan 02 '22 at 11:47
  • "because my team-leader and client demanding it" - No, I was asking why you are explicitly triggering the 404 in the code you posted... `Redirect 404 /notfound.php` doesn't really make sense. How the 404 (or 403) is being triggered alters how you solve this problem. If you are explicitly triggering the 404/403 response in PHP then you can't resolve this using Apache. However, if the 404 is simply the result of a file not existing and you aren't rewriting the request then yes, this can be resolved in Apache. Likewise, how is the 403 being triggered? – MrWhite Jan 02 '22 at 12:41
  • "because my team-leader and client demanding it" - But are they literally wanting you to override this response (which is arguably quite "silly") or are they wanting you to resolve why the 404 or 403 response is being triggered in the first place? That is a different matter. – MrWhite Jan 02 '22 at 12:43
  • 1
    @MrWhite my clients want overwrite of response code with htaccess. Can you help me with htaccess code which overwrite the response code :) – Stellan Coder Jan 02 '22 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? – MrWhite Jan 02 '22 at 14:45
  • @MrWhite from browser. Like a normal. if someone access a link which do not exist it will provide 404 error but i want same thing but response code is 200 instead of 404 – Stellan Coder Jan 02 '22 at 16:04
  • 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? – MrWhite Jan 02 '22 at 16:28
  • 1
    I’m going to take you at your word that you’ve been given this very specific task, however I would consider this the same as “drive this screw in with this hammer”, and I would report back that this isn’t recommended, or possibly not even possible, however the fix should be pretty easy in PHP. If you get pushback, then ask for help from the team leader, because if they told you to do this, then I would assume they know how to do this (although they probably don’t). – Chris Haas Jan 02 '22 at 16:59
  • 1
    The code you are using appears to be a variant of [this](https://stackoverflow.com/a/15088830/231316). As @MrWhite alluded to, that code is for Apache-level 404s, however, if PHP is invoking the 404, than Apache is going to honor that. – Chris Haas Jan 02 '22 at 17:03
  • @MrWhite yes, i used that guide and there is no PHP header function i declared. I am not overriding apache with PHP. all i am trying to do with .htaccess – Stellan Coder Jan 02 '22 at 18:47
  • 1
    @MrWhite you closed my question which do not have any answer yet and marked duplicate by the another my own question which also do not have proper solution for my questions :( – Stellan Coder Jan 08 '22 at 17:20
  • 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. – MrWhite Jan 08 '22 at 17:29
  • 1
    @MrWhite Got it. anyway do you have any solution for it. i googled several website and also searched other part of stackoverflow but still no solution :( – Stellan Coder Jan 08 '22 at 18:10
  • Maybe this answer helps https://stackoverflow.com/a/15088830/1741542 – Olaf Dietsche Jan 08 '22 at 19:40

1 Answers1

1

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]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    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. – RavinderSingh13 Jan 09 '22 at 10:06
  • 1
    Yes that's right @RavinderSingh13. `REDIRECT_STATUS` is set due to use of `ErrorDocument` directive. – anubhava Jan 09 '22 at 10:08
  • 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. – RavinderSingh13 Jan 09 '22 at 10:10
  • 1
    Personally I would just place a custom 404 page for viewers but OP had different requirements – anubhava Jan 09 '22 at 10:13
  • 1
    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. – RavinderSingh13 Jan 09 '22 at 10:15
  • @shanmugapradeep: Did this answer work for you? – anubhava Jan 17 '22 at 09:42