简体   繁体   中英

HTTP-404 header doesn't seem to work in server log

I've built a dynamic website and modified the .htaccess file for static URLs and I need to create a 404 page for SEO.

header("HTTP/1.0 404 Not Found");

I'm using the above PHP header() function when there is no matching link in database, but when I check the server logs it seems like this does not work and gives HTTP 200 OK status code.

Here's the line from the server log:

[My IP] - - [12/Jun/2011:01:47:38 +0300] "GET /4k.html HTTP/1.1" 200 3284 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.91 Safari/534.30" 

4k.html is the link which does not exist.

Am I missing something?

From what I understand, you are rewriting all files to a single PHP file so PHP can read it and do what it needs to do to serve a page. That means that Apache's handling of 404 pages is going to be, essentially, overridden. At this point it is up to your PHP file to serve a page or display a 404. As far as Apache knows, the request resulted in an HTTP code of 200, because it doesn't play any part after it triggers PHP to parse the script and give the output to the client that requested it.

In your PHP script, you will be doing checking to see if the page exists or not. If the page exists, show the page, else, give a 404 message. By putting header("HTTP/1.0 404 Not Found", true); in your PHP file at the point where you know it's a 404, it will send the 404 header to the client, and therefore it was done correctly. To verify it is being sent correctly, trigger a 404 (by putting in a bogus url) and view the request details in Chrome Developer tools, you should see under Response headers that you are being sent a 404, while your Apache logs display a 200.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM