Apache – Fixing Slow Image Load Due to .htaccess

.htaccessapache-2.4

I am running a single page application that requires the following lines on my .htaccess . However, images on its /images/ folder are now loading extremely slowly.

Is there anything wrong with the following lines?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.html [QSA,L]

Best Answer

This should not have a significant impact on the loading of images. However, it can be optimised to exclude the /images directory from being processed.

For example:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^images/ index.html [L]

The QSA flag was not required.

OR, exclude all requests that look like they already have a file extension, which should naturally include all your images, JS, CSS and any other static resources. However, this does mean that your page URLs cannot contain what looks like a file extension.

For example:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.\w{2,4}$ index.html [L]
Related Topic