1

here is my location block

  location / {
             auth_basic "Restricted Content";
             auth_basic_user_file /etc/nginx/.htpasswd;
             try_files $uri $uri/ /index.php;

         }

I have set of routes. for example, /blog, /contact. basically there is only one index.php page. there is no blog or contact page.

when user hits url/blog I am loading the index.php file and on javascript I am showing/hiding the content based on the url parameter.

when no predefined route matched for exmaple url/rgdf I am showing a 404 div to browser.

but now the problem is response header is 200 on 404 page. I want to set the response header to 404

how can I achieve that?

Ashik
  • 2,888
  • 8
  • 28
  • 53
  • It is your `index.php` that should respond with the 404 HTTP code for a non-existent route (using something like `http_response_code(404);`). – Ivan Shatsky Feb 17 '22 at 18:37
  • problem is there is only one page (index.php) in the whole site, what I am doing is hiding/showing divs by button click with the help of js. so when some non existing route hits, currently I am showing the 404 div but the response header is 200. what I want is to make the header 404 as long as the 404 div is shown – Ashik Feb 19 '22 at 06:00
  • 1
    I'm not sure you understand how is HTTP protocol works. If you are manipulating DOM with the javascript, all your HTML content already loaded, it does not reloading again and again. Request body is being sent after the HTTP response code and all the response headers, you can't change it after this happens. That means if you want to set the response code to 404, you should do it before any page content output. – Ivan Shatsky Feb 19 '22 at 10:37
  • Further reading: [How to fix "Headers already sent" error in PHP](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Ivan Shatsky Feb 19 '22 at 10:38

0 Answers0