How to add Leverage browser caching for CDN in .htaccess?

How to add Leverage browser caching for CDN in .htaccess?

July 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add Leverage browser caching for CDN in .htaccess?

Maximizing Performance: Configuring Leverage Browser Caching for CDN in .htaccess

In today’s fast-paced web environment, every millisecond counts when it comes to loading times and user experience. One of the most effective ways to optimize your website is through browser caching and Content Delivery Networks (CDNs). In this article, we’ll guide you step-by-step on how to configure leverage browser caching for a CDN in your .htaccess file.

Understanding Browser Caching

Browser caching allows the user’s browser to store static files like images, scripts, and stylesheets locally. When these files are needed again, they can be served directly from the cache, eliminating the need for a server request. This significantly speeds up page load times, especially for repeat visitors.

Configuring Leverage Browser Caching in .htaccess

You’ve already got a great start with your current .htaccess configuration. However, to fully leverage browser caching for a CDN, we’ll need to add some additional directives that specify the cache control headers for the resources served by the CDN.

<IfModule mod_expires.c>
ExpiresActive On

# Cache images for 1 year
ExpiresByType image/jpg "access plus 365 days"
ExpiresByType image/jpeg "access plus 365 days"
ExpiresByType image/gif "access plus 365 days"
ExpiresByType image/png "access plus 365 days"

# Cache CSS and JavaScript for 1 month
ExpiresByType text/css "access plus 30 days"
ExpiresByType application/x-javascript "access plus 30 days"

# Cache other file types for 2 days
ExpiresDefault "access plus 48 hours"
</IfModule>

What’s Changed?

The main change is the addition of access plus syntax in place of access. This tells Apache to calculate the expiration time relative to when the resource was last modified, rather than the time it was requested.

For CDN assets, this ensures that if a file changes on the origin server, the updated version will be served immediately on subsequent requests.

Best Practices for Leverage Browser Caching

  1. Be Specific: Use ExpiresByType to specify different expiration times for different types of resources.
  2. Balance Aggressiveness: Longer cache durations reduce server load but may not reflect changes quickly. Find a balance that works for your specific needs.
  3. Minimize File Changes: Frequent updates can negate the benefits of caching.
  4. Monitor and Test: Regularly check your .htaccess file to ensure it’s still configured correctly, especially after updates or migrations.

Conclusion

Optimizing browser caching is a powerful way to improve your website’s performance. By leveraging browser caching for CDNs in your .htaccess file, you can significantly reduce load times and enhance the user experience.

Remember, every website is unique, so feel free to experiment with different cache durations to find what works best for you. If you’re looking for more advanced techniques or commercial solutions, consider checking out Laravel Company, which specializes in providing high-quality Laravel development services and performance optimizations.

Happy optimizing! Let us know if you have any questions or need further clarification.

Cache Performance Extensions Themes

Our network