Apache Expires Header Example For Essay

On By In 1

YSlow: Add Expires headers

Overview

Expires headers let the browser know whether to serve a cached version of the page.

  • Reduce server load
  • Decrease page load time
  • Cost benefit ratio: high value
  • Access needed

How does your site score on this recommendation?

What are expires headers

Expires headers tell the browser whether they should request a specific file from the server or whether they should grab it from the browser's cache.

The whole idea behind Expires Headers is not only to reduce the load of downloads from the server (constantly downloading the same file when it's unmodified is wasting precious load time) but rather to reduce the number of HTTP requests for the server.

When you visit a website your browser is responsible for communicating with the web server to download all the required files. It then compiles those files to display the web page. As web pages become richer in graphics and content, more and more files are being transferred between your machine and the web server.

In the past you would have an HTML file and maybe a few images to serve for your website, however many modern websites might have 50+ files per page to transfer. The files themselves can be a huge load increase by themselves but for each file you must create a request and even if requests are fractions of a second, they can soon add up.

How does it work?

Expires Headers are rather simple in how they work. They tell the browser how long to store a file in the cache so subsequent page views and visits they don't have to download the file again. You are right to assume Expires Headers don't improve page speed for a first time visit as this visitor would have to download all the files for the first time. Using Expires Headers helps decrease load times for returning visitors.

You can set Expires headers on specific files or even file types. Then when the browser comes to the website it can see when was the last time it downloaded the specific file types. If it was recently it will display them from the cache, if you haven't visited the site in a while it will download the newest version from the web server.

The idea is to set late expiry times for items that don't change on your website (logo, colours etc). Set short expiry times for things that change regularly.

Why is it important?

Adding Expires Headers is important to reduce HTTP requests which reduces the time it take for the server to communicate with the browser. It also allows your users to reuse the cache files that have been stored in the browser to reduce the amount of files they need to download.

How to Add Expires Headers

First you need to decide on what files you update often and what file types don't get updated often. A common list of file types we see a lot are:

  • images: jpg, gif, png
  • favicon/ico
  • javascript
  • css

Now go through these file types and think how often you change each one. Commonly images typically are not changed too often (keep in mind we are talking about existing images) favicon is almost never changed, javascript is occasionally changed and CSS is change much more frequently.

We define our Expires Headers in the .htaccess file. This is a hidden file often found in the root of your website (via FTP). It's always best practice before you edit the htaccess file to back it up!

Now, open up your htaccess file and paste in the following:

<IfModule mod_expires.c> # Enable expirations ExpiresActive On # Default directive ExpiresDefault "access plus 1 month" </IfModule>

First we enable expirations and then we set a default expiry date for files we don't specify. Now we want to add the lines to explain what expires when. Right above the </IfModule> paste the following and change the dates to reflect the times that best suit your website.

# My favicon ExpiresByType image/x-icon "access plus 1 year" # Images ExpiresByType image/gif "access plus 1 month" ExpiresByType image/png "access plus 1 month" ExpiresByType image/jpg "access plus 1 month" ExpiresByType image/jpeg "access plus 1 month" # CSS ExpiresByType text/css "access plus 1 month" # Javascript ExpiresByType application/javascript "access plus 1 year"

Your complete file should look like:

<IfModule mod_expires.c> # Enable expirations ExpiresActive On # Default directive ExpiresDefault "access plus 1 month" # My favicon ExpiresByType image/x-icon "access plus 1 year" # Images ExpiresByType image/gif "access plus 1 month" ExpiresByType image/png "access plus 1 month" ExpiresByType image/jpg "access plus 1 month" ExpiresByType image/jpeg "access plus 1 month" # CSS ExpiresByType text/css "access plus 1 month" # Javascript ExpiresByType application/javascript "access plus 1 year" </IfModule>

There are a few common causes for this error code including problems with the individual script that may be executed upon request. Some of these are easier to spot and correct than others.

File and Directory Ownership

The server you are on runs applications in a very specific way in most cases. The server generally expects files and directories be owned by your specific user cPanel user. If you have made changes to the file ownership on your own through SSH please reset the Owner and Group appropriately.

File and Directory Permissions

The server you are on runs applications in a very specific way in most cases. The server generally expects files such as HTML, Images, and other media to have a permission mode of 644. The server also expects the permission mode on directories to be set to 755 in most cases.

(See the Section on Understanding Filesystem Permissions.)

Note: If the permissions are set to 000, please contact our support team using the ticket system. This may be related to an account level suspension as a result of abuse or a violation of our Terms of Service.

IP Deny Rules

In the .htaccess file, there may be rules that are conflicting with each other or that are not allowing an IP address access to the site.

If you would like to check a specific rule in your .htaccess file you can comment that specific line in the .htaccess by adding # to the beginning of the line. You should always make a backup of this file before you start making changes.

For example, if the .htaccess looks like

Order deny,allow
allow from all
deny from 192.168.1.5
deny from 192.168.1.25

Then try something like this

Order allow,deny
allow from all
#deny from 192.168.1.5
deny from 192.168.1.25

Our server administrators will be able to advise you on how to avoid this error if it is caused by process limitations. Please contact our Live Support or open a Ticket. Be sure to include the steps needed for our support staff to see the 403 error on your site.

0 comments

Leave a Reply

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