Working with Apache files expires and Apache set caches

Tutorials Apache

Need to set Apache cache for your website to improve performance? Our Developers can help you today.

Setting the Cache of your website determine how your Website visitor sees it on a web browser and this depends on how Apache's Mod_expires module is configured.

Here at Fixwebnode, as part of our Website Support Services, we regularly help our Customers to fix Apache and Website errors on a daily basis.

Today, we shall look into how to set caches with Apache Web Server.

 

More about Apache caches?

Caching via a Web Server is a method of enhancing the performance of a Server by allowing commonly requested content to be temporarily stored in a way that allows for faster access. This process speeds up processing and delivery of data by cutting out some resource intensive operations. Apache cache module allows you to cache data easily by means of shared memory.

 

How to configure caches with Apache?

To set caches with apache web server, you can configure the Cache-Control HTTP headers for your website. 

To achieve this, you need to add "mod_expires" in the ".htaccess" file of your Website or server.

In cases where your website cannot be accessed via Cache Control, the website will make a request to the server to access images, css files, javascript files used in the website.

You can add the rules as shown below to your .htaccess file which is located in the root directory of your website. If this file does not exist, you can simply create one and add the line below;

ExpiresActive on

This should be followed by the following rules;

ExpiresDefault "base[plus num type] [num type] ..."
ExpiresByType type/encoding "base[plus num type] [num type] ..."

This means that the ExpiresDefault and ExpiresByType directives is implemented which functions to allow Apache Expires Module to take effect.

The "ExpiresDefault" module helps to set caches for all files in the Server. 

This module specifies the default algorithm meant to keep track of the expiration time relating to all the affected realm. 

To set caches by type, use the "ExpiresByType" directive as seen on the directives above. This directive signifies the Expires header's value and the max-age directive available in the Cache-Control header generated for documents of a specified type.

Therefore, the "ExpiresByType" directive only takes effect when "ExpiresActive On" is implemented. Relatively, it overrides any expiration date set by "ExpiresDefault" when a specified MIME type is specified.

In the above directive, "base" belongs to;

i. access

ii. now (which is equivalent to "access")

iii. modification

 

"plus" there is an option feature. 

"Num" represent an integer value working with atoi().

"Type" can be one of the following;

i. years

ii. months

iii. weeks

iv. days

v. hours

vi. minutes

vii. seconds

 

To set file types and times for caches, add the following directives to the .htaccess file;

ExpiresActive on
# ExpiresByType text/html "access plus 900 seconds"
ExpiresByType text/css "access plus 60 days"
ExpiresByType text/javascript "access plus 60 days"
ExpiresByType application/javascript "access plus 60 days"
ExpiresByType application/x-javascript "access plus 60 days"
ExpiresByType image/gif "access plus 14 days"
ExpiresByType image/jpg "access plus 14 days"
ExpiresByType image/jpeg "access plus 14 days"
ExpiresByType image/png "access plus 14 days"
ExpiresByType application/x-shockwave-flash "access plus 14 days"
# Add a far future Expires header for fonts
ExpiresByType application/vnd.ms-fontobject "access plus 2 year"
ExpiresByType application/x-font-ttf "access plus 2 year"
ExpiresByType application/x-font-opentype "access plus 2 year"
ExpiresByType application/x-font-woff "access plus 2 year"
ExpiresByType image/svg+xml "access plus 2 year"

You can use the FilesMatch directive to implement cache for your files as shown below;

<FilesMatch "\.(gif|jpe?g|png)$">        
   Header set Cache-Control "public"    
</FilesMatch>
<FilesMatch "\.(html)$">        
   Header set Cache-Control "public"    
</FilesMatch>
<FilesMatch "\.(php)$">
   Header set Cache-Control "private"    
</FilesMatch>

 

Need support in fixing Apache Web Server Errors? We are available to help you today.

 


Conclusion

How to set Caching on Apache Web Server implemented on the .htaccess file located in the website root directory ? Apache HTTP Web Server is a free and open-source web server that delivers web content through the internet. In this article, we will show you how to configure a basic apache caching by modifying the .htaccess file.

Your Cart