Cache Pages Without A Plugin

Posted on

Caching pages is beneficial because it improves website performance by storing static copies of pages or resources. This reduces server load and speeds up user access, enhancing overall user experience.

Caching without a plugin can be better in some cases as it allows for more control and customization. Direct server-level caching or using code-based solutions can be tailored to specific needs, ensuring optimal performance for unique requirements without relying on third-party plugins that may have limitations.

Additionally, caching without a plugin can offer greater flexibility in terms of implementing custom cache expiration strategies and handling specific content types. This level of control enables website developers to fine-tune caching mechanisms based on the nature of their content and user interaction patterns, resulting in a more tailored and efficient caching approach.

Cache Pages Without a Plugin

Caching a website using .htaccess involves leveraging Apache server directives to control caching behavior. You can set expiration times for different types of resources to improve load times, and using a Cache-Control header. Here’s a basic example:

First Add at the top of htaccess

Header always set Cache-Control "public, max-age=63072000, immutable"

Followed by:

IfModule mod_expires.c>
ExpiresActive On

# Images
ExpiresByType image/jpeg "access plus 2 year"
ExpiresByType image/gif "access plus 2 year"
ExpiresByType image/png "access plus 2 year"
ExpiresByType image/webp "access plus 2 year"
ExpiresByType image/svg+xml "access plus 2 year"
ExpiresByType image/x-icon "access plus 2 year"

# Video
ExpiresByType video/webm "access plus 2 year"
ExpiresByType video/mp4 "access plus 2 year"
ExpiresByType video/mpeg "access plus 2 year"

# Fonts
ExpiresByType font/ttf "access plus 2 year"
ExpiresByType font/otf "access plus 2 year"
ExpiresByType font/woff "access plus 2 year"
ExpiresByType font/woff2 "access plus 2 year"
ExpiresByType application/font-woff "access plus 2 year"
ExpiresByType application/font-woff2 "access plus 2 year"

# CSS, JavaScript
ExpiresByType text/css "access plus 2 year"
ExpiresByType text/javascript "access plus 2 year"
ExpiresByType application/javascript "access plus 2 year"

# Others
ExpiresByType application/pdf "access plus 2 year"
ExpiresByType image/vnd.microsoft.icon "access plus 2 year"
</IfModule

This example sets default caching for HTML to 2 years, CSS to 2 years, JPEG and PNG images to 2 years, and JavaScript to 2 years. Adjust these values based on your website’s update frequency. Remember, modifying .htaccess requires careful attention, and backup the file before making changes to avoid unintended issues.