Now Reading
How to Improve Website Loading Speed with .htaccess

Enhancing Website Speed with .htaccess: A Comprehensive Guide

In the realm of website optimisation, the .htaccess file serves as a powerful tool for enhancing speed, security, and performance. Let’s explore various techniques that can be implemented using .htaccess to bolster your website’s speed.

1. Enable GZIP Compression

GZIP compression reduces the size of files transferred between the server and the client’s browser, significantly improving load times. Add the following code to your .htaccess file:

<IfModule mod_deflate.c>
  # Enable GZIP compression
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/json
</IfModule>

2. Leverage Browser Caching

Improve load times for returning visitors by instructing the browser to cache certain resources. Add the following directives to set expiry times for various file types:

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpg "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType application/pdf "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
  ExpiresByType application/x-javascript "access plus 1 month"
  ExpiresByType image/x-icon "access plus 1 year"
  ExpiresDefault "access plus 2 days"
</IfModule>

3. Redirects and Rewrites for Efficient Resource Handling

Redirects and URL rewriting can help in managing resources efficiently. For example, implement 301 redirects to ensure consistent URLs and use URL rewriting to create cleaner, SEO-friendly URLs:

# Redirect non-www to www (or vice versa)
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# URL rewriting for clean URLs
RewriteRule ^products/([a-zA-Z0-9_-]+)$ product.php?slug=$1 [L]

4. Minimise Server Requests

Reducing the number of HTTP requests can significantly improve website speed. Combine CSS and JavaScript files and limit unnecessary elements:

# Combine CSS files
<FilesMatch "\.css$">
  Header set Connection keep-alive
</FilesMatch>

# Combine JavaScript files
<FilesMatch "\.js$">
  Header set Connection keep-alive
</FilesMatch>

5. Browser Caching for Web Fonts

Web fonts contribute to page weight. Set longer caching periods for web fonts to reduce server requests:

<IfModule mod_headers.c>
  # Set longer cache for web fonts
  <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css)$">
    Header set Cache-Control "max-age=31536000, public"
  </FilesMatch>
</IfModule>

Implementing these .htaccess configurations can significantly enhance your website’s speed and performance. Remember to back up your .htaccess file before making any changes and test your website thoroughly after each modification to ensure optimal performance across different browsers and devices.

What's your reaction?
Love It
0%
Like It
0%
Want It
0%
Had It
0%
Hated It
0%
About The Author
BSEO