My static files like images, css, or txt files are failing to load with a 404 error or failing to redirect Print

  • 0

Problem Description

A static file is defined as any file with an extension which most commonly remains unchanged. Example file extensions: .txt, .html, .pdf, .jpg, .png, .css, etc. This means all images, pdfs, text files, and more.

Any combination of the following may be true:

  • You're using a web application (ex: PrestaShop or WordPress Multisite) and/or you have a .htaccess file with static file rewrites
  • All or some static files are not showing / loading on your website (for PrestaShop it's only product images).
  • You're using software that links to a file that does not exist on disk (dynamically generated) and which, based on its file extension, should be static and therefore should be found as a file on the server (example: ads.txt)
  • You are attempting to create a redirect via a plugin, PHP, or .htaccess, where the source is a static file, but the redirect does not function.
  • When you check the browser console or server logs, it shows 404 errors when loading images or other static files. The browser console and server log will provide the path to the image. When you check the path to the image file on the server using Plesk File Manager or FTP, the image really doesn't exist at that location

This means that your site is attempting to using apache rewrites to serve the static files by feeding their request into PHP - or another server-side language - rather than loading them directly from the disk.

When static files are handled by PHP (such as with WordPress multisite, Prestoshop, and other web apps), this means every single image request will load a PHP process to handle it, which is thousands of times slower than simply loading the file from disk. To prevent that from causing problems with site performance, our servers are optimized to provide a basic 404 page when the image file does not exist at the location on disk, thus blocking PHP processing, and .htaccess rewrites, for any static resources that require rewrites to load.

Problem Resolution

There are two ways to resolve this:

A) Try to configure the web app / rewrites so that you don't need dynamic processing (usually from PHP) to load the static files

The best solution for the performance of your website is to check your web application's documentation and support articles to determine if there is a configuration for the app that will generate your site's HTML using the correct URLs to the image path on disk, rather than using a fake/friendly URL that then needs PHP processing to load them.

Example for Prestashop: in the admin, go to Preferences (or Shop Settings) > SEO & URLs > Disable Friendly URLs

In the case of redirects, you can ask us to implement the redirect with nginx so that it occurs prior to our 404 optimizations. This is the fastest method of redirect.

B) Lose the performance optimization by disabling optimized 404 handling:

If there's no way to force the app to use the actual path on disk for these files (boo!), you can bypass our optimizations by adding a blank file called .dynamic404 (it must be preceeded with a dot) in your website's web root folder (for primary domains, this is usually within httpdocs).

IMPORTANT NOTE: If you use option (2) and you have an increase in traffic your site will very likely exhaust its PHP resources fast. If this occurs, your only good solution is to migrate to a VPS. On the flip side, if you find a way to directly link to the file on disk, your site will very likely be able to handle far more traffic without requiring an upgrade of service.


Was this answer helpful?

← Back