Server returns 404 for a web page, but page is showing fine in browser - why?

Viewed 3753

A strange web page crossed my way. (And being a developer I have to solve the mystery.)

When accessing the web page in any browser, all seems normal. The web page is displayed as expected.

But when looking in the console the server acually returns a 404 status code:

enter image description here

So why is the browser rendering a page?

Looking at the Body shows valid HTML is returned:

enter image description here

Hold on. Responding 404 and sending the HTML along the way? And the browser renders it??

Why is this happening? Is this some server misconfiguration? Or is something clever going on here that I don't understand? Is there a practical reason for configuring a server on purpose to behave like this?

3 Answers

I faced with the same situation. My portal was hosted in a tomcat server. The portal was loaded when the host name along with the tomcat directory path was hit. But on loading the the webpage redirected to a deep-link URL and rendered the page. But if you hit the deep link URL directly in the browser it would give you 404 error in the network tab in Dev tools although the webpage would be rendered fine. This happens because there is no resource as your deep-link URL anywhere in your server config files, so when it searches for the resource it doesn't find one and returns 404 in the network tab in Dev tools. But browser behaves differently with the resource URL. It first loads and connects to the host name of the resource, when returned with success gets redirected as per the config files settings and renders the deep-link URL resource HTML, styling contents properly.

Note: I don't know whether this issue comes from me not being strict enough in the .htaccess or my CMS.

In my contrived .htaccess example I had the following rules to ignore these directories from being handled by the CMS.

RewriteCond $1 !^(branch|css|js|html|images) [NC]

I also had a branches directory inside my CMS' templates (created within CMS). I guess my .htaccess rule wasn't strict enough here. I had to change branch to branch\/, like so:

RewriteCond $1 !^(branch\/|css|js|html|images) [NC]

Only then would the page load without the 404 in the console.

Related