Hostwinds Blog


304 Status Code: Everything You Need to Know Featured Image

304 Status Code: Everything You Need to Know

by:   /  January 6, 2024


What is 304 Not Modified Response?

The HTTP 304 Not Modified status code is a redirection code indicating that the requested resource has not been altered since the last request.

In simple terms, the server is telling the browser that the page/resource it's requesting has not been updated/changed since the browser's last visit. So instead of re-sending and re-downloading the page/resource, which takes time and consumes bandwidth, the browser is told it can show the existing page/resource stored in its cache.

How Does 304 Status Code Work?

For a server to respond with a 304 Status Code, it needs to know if the resource stored in the browser's cache is the same version stored in its database.

Here's how that works:

Caching

When a browser visits a webpage for the first time, it's requesting resources stored by the site's web server.

The web server responds to the request with a 200 OK HTTP code and sends the resources to the browser. Included in that response, within the HTTP header, is a hash code or Etag (Entity tag) - a unique identifier, typically a series of numbers, that changes whenever a website/page/resource is modified or updated.

Along with caching the webpage content, the browser also records the time/date of its request.

Conditional Request

Upon revisiting the same webpage, the browser initiates a conditional HTTP request,  telling the web server that only under certain conditions should it gather the webpage resources from the server database and send it for the browser to download.

There are two basic HTTP headers used for conditional requests, each containing information about the version of the webpage currently stored in the browser's cache:

"If-Modified-Since" contains the date/time the browser last requested the webpage.

"If-None-Match" contains the Etag (hash code).

Server Response

The web server will take the conditional request and compare the HTTP header information with the information in its database, returning either 200 OK or 304 Not Modified status.

When "If-Modified-Since" is found in the HTTP header, the web server will compare the date/time the webpage was updated on its end to the date/time it was cached by the browser.

When "If-None-Match" is found in the HTTP header, the web server will see if the webpage's Etag in its database matches the Etag of the browser's cached version of the webpage.

When both headers are being used for verification, "If-None-Match" will take precedence over "If-Modified-Since."

If the web server information matches the header information, the web server will respond with a 304 Not Modified HTTP code, telling the browser to show the cached version of the webpage. If the information doesn't match, the server will retrieve the updated webpage and send it to the browser where it will then be downloaded and cached for a future conditional HTTP request.

Is 304 Response Code Important?

The value of the 304 HTTP code really depends on the size of the website.

For large websites (tens of thousands of pages), the 304 response code can help reduce bandwidth usage and increase site speed for better user experience. It could also help with SEO by freeing up crawl budget and potentially allow faster indexing - search engine bots don't typically recrawl pages they know haven't been changed, allowing them to find and crawl new and/or updated pages quicker.

Smaller sites could also benefit from the 304 HTTP code's caching proficiencies but the advantages are pretty negligible.

That said, small sites can experience excessive amounts of traffic, quickly consuming bandwidth, in which case the 304 code could be potentially beneficial. However, there are much better ways to mitigate such an issue.

Is HTTP 304 Status Code Bad?

The 304 status code is a great asset for optimizing web performance and saving bandwidth through the avoidance of redundant data transfers.

There are some that liken it to an error code (4xx or 5xx) but it's not. It is a redirection code used by browsers (clients) and servers to communicate and execute an action.

That said, there can be situations where communication between client and server is interrupted or broken. Let's call this issue a Stale 304.

Client Side

If a user is constantly encountering a 304 Not Modified status code from a site they frequent, it could indicate an issue with the browser in that it's unable to properly execute the conditional HTTP request and download the updated content.

This issue typically stems from outside sources connected to the browser, such as corrupted files in browser-based applications, corrupted files in recently downloaded software, and viruses or malware.

Any one of these sources could corrupt the browser's cache and/or affect how the browser communicates with the web server.

Server Side

a Stale 304 may be due to an issue with the web server's redirect settings.

Redirect settings are directives found in the .htaccess file system that use the 3xx status codes to control how web traffic is directed or rerouted on a site.

Improperly configured 304 status code directives could lead to stale content delivery, inefficient use of server resources, or even browser compatibility issues.

5 Ways to Fix a Stale 304 Status Code

Fortunately, there are actions that can be taken to resolve or even outright avoid a Stale 304.

Clear Browser Cache

The most common and effective method is deleting all browsing history, cookies and any other cached data.

All previously stored web pages are removed and websites are no longer storing browser data. When a user visits a site it's as if they're visiting for the first time and now have a fresh copy of the site.

Flush DNS Cache

DNS Cache works in the same manner of storing data like browser cache, but on the operating system. It too holds browsing data, along with IP addresses, hostnames and resource records.

The additional stored data allows web pages to load faster as the browser doesn't need to pass through the entire Domain Name System process.

And just like clearing browser cache, flushing DNS cache basically resets the web surfing experience, allowing users (browsers) to collect fresh cacheable content.

Scan for Viruses and Malware

Browsers infected with malware or viruses corrupt browser cache files and interrupt communication with web servers.

Running a scan for viruses and malware can help identify and remove the threat, allowing the browser to resume normal operations.

Disable/Remove Browser Extensions

We all love a good browser extension, but some extensions may contain corrupted files that make their way into the browser's cache, affecting how the HTTP headers communicate with those of the web server's.

Reviewing extensions and disabling or removing them one by one can help pinpoint the culprit.

Review .htaccess Redirect Instructions

On the server side we have the .htaccess.This is a server configuration file that controls the behavior of a website at the directory level, including how to handle 304 status codes.

Reviewing the redirect instructions in the file may show an issue with how the web server is communicating 304 status codes to browsers.

Conclusion

The HTTP 304 Not Modified status code is designed to help streamline communication between clients (browsers) and servers, and when working properly does help optimize web performance and save bandwidth.

However, if not configured correctly or corrupted by outside forces, the 304 status could create a stale content issue, affecting user experience and search engine rankings.

At the end of the day, we have a number of solutions that can help resolve or hopefully outright avoid the negative effects of the 304 status code.

Remember, HTTP 304 Status code is NOT an Error.

Happy Hosting!

Written by Hostwinds  /  January 6, 2024