Fast and Easy Changes to Apache .htaccess to Increase Web Page Speed

Apache-Web-ServerThere are lots of reasons why having web pages load faster is something to work toward but how to get the speed and what to focus on can be hard to figure out. Here are some easy tips to increase speed and cut down page load times.

The first thing to do is enable both compression of page contents and set an expiration date the items delivered to browsers. The first gets the content there faster and the second makes sure returning visitors don’t have to load the same content each time they visit. For this we will assume that your site runs Apache on Linux.

In the root folder of your website, usually httpdocs there probably exists a file called .htaccess. This gives Apache instructions on how to operate. Because the file starts with a dot it is not seen with a standard FTP client or ls command. If you are secure shelled in you can check for it with the more command:

# more .htaccess

If this command shows the contents of the file then it’s there, if it doesn’t then create the file. For shell access just vi the file name (even if it doesn’t exist) and paste the following into it, but make a copy of the file first:

<IfModule mod_deflate.c>
SetOutputFilter DEFLATE

# Do not compress
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary

#Dealing with proxy servers
<IfModule mod_headers.c>
Header append Vary User-Agent
</IfModule>

</IfModule>
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 hour"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##


If you’re using an FTP client then set it to show all files, download the original and append this to the file. Keep a copy of the original file. You can change these as needed. For example the html line is customized to give a shorter life so that frequent page changes are picked up by return visitors.

Changes to .htaccess do not require a restart of Apache.

For a good before and after run Google PageSpeed before any changes and then again after changes. A more in depth test from a geography of your choosing can be done with WebPagetest.

If something goes wrong just replace the modified file with the copy you made.

Image: Apache Foundation

Leave a comment

Your email address will not be published. Required fields are marked *