How to Speed up WordPress Using .htaccess in 2024

We have covered many topics before on how to speed up your site using various methods like cache, plugins, core web vitalimage optimization, and code optimization. But I have never written an article on .htaccess.

It certainly is a unique approach to speeding up your site, and of course, it’s technical. But don’t worry about the methods; the ways that are going to be presented here will be easy to understand and implement.

So let’s get started!

Why and What is .htaccess?

Let’s first understand what is .htaccess.

In the main directory of Apache web servers, you’ll discover a configuration file named .htaccess. 

This file empowers users to exert control over designated folders or server elements. By altering the .htaccess file, one can adjust the server application’s configuration to activate or deactivate specific functionalities and features.

Now let’s understand Why we use the .htaccess file to optimize.

We use the .htaccess file for optimization purposes due to its capability to control and fine-tune various aspects of server configuration. This file empowers us to enhance website performance, security, and functionality by implementing specific directives and rules. 

Whether it’s enabling caching, redirecting URLs, enhancing security measures, or optimizing resource delivery, the .htaccess file serves as a powerful tool to customize and optimize the behavior of an Apache web server, ultimately contributing to a better overall web experience for users.

Method 1: Gzip File Compression

Gzip Compression is a technique that makes websites load faster by reducing the size of the data sent over the internet. Gzip is a commonly used method to compress various types of files like HTML, scripts, and style sheets.

Here’s how you can implement Gzip compression:

With .htaccess (for servers with Apache)

If your server supports the mod_pagespeed module, you can add the following code to your .htaccess file:

<IfModule pagespeed_module>

    ModPagespeed on

    # using commands, filters, etc.

</IfModule>

For servers that don’t support mod_pagespeed but use Apache, you can use mod_deflate. Add this code to your .htaccess file:

<ifModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/css text/javascript application/javascript application/x-javascript
</ifModule>

With header.php (for servers without mod_deflate or mod_gzip)

If your server doesn’t support mod_deflate or mod_gzip, you can use a PHP script for gzip compression. Insert the following code into the header.php file of your theme:

<?php

if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {

    ob_start("ob_gzhandler");

} else {

    ob_start();

}

?>

Method 2: To Turn Off ETags

ETags (entity tags) are used by web servers and browsers to check if a component in the browser cache matches the original. They are more flexible than checking the last change date because ETags are strings identifying a particular version of the component.

To disable ETags, add the following lines to your.htaccess file:

<IfModule mod_headers.c> 

    Header unset ETag 

</IfModule> 

FileETag None

This code removes the ETag header and sets the FileETag to None, effectively turning off ETags for your website.

If, for some reason, you want to re-enable Etags, just remove the code.

Method 3: Browser Caching

Using browser caching involves instructing the browser to store certain files for a specific period. Instead of fetching these files from the server every time, the browser retrieves them from its local cache.

Imagine a website without caching. It is like driving to the store with a glass of water every time you’re thirsty – impractical, short-sighted, and a lot of effort!!

Browser caching is not only convenient for users but also encourages them to return, creating a base of loyal visitors and saving bandwidth.

For servers with Apache, you can employ the mod_expires and mod_headers modules to set up caching rules in your .htaccess file:

# BEGIN Expire headers

<ifModule mod_expires.c>

    ExpiresActive On

    ExpiresDefault "access plus 5 seconds"

    ExpiresByType image/x-icon "access plus 2592000 seconds"

    ExpiresByType image/jpeg "access plus 2592000 seconds"

    ExpiresByType image/png "access plus 2592000 seconds"

    ExpiresByType image/gif "access plus 2592000 seconds"

    ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"

    ExpiresByType text/css "access plus 604800 seconds"

    ExpiresByType text/javascript "access plus 216000 seconds"

    ExpiresByType application/javascript "access plus 216000 seconds"

    ExpiresByType application/x-javascript "access plus 216000 seconds"

    ExpiresByType text/html "access plus 600 seconds"

    ExpiresByType application/xhtml+xml "access plus 600 seconds"

</ifModule>

# END Expire headers

# BEGIN Cache-Control Headers

<ifModule mod_headers.c>

    <filesMatch "\.(ico|jpe?g|png|gif|swf)$">

        Header set Cache-Control "public"

    </filesMatch>

    <filesMatch "\.(css)$">

        Header set Cache-Control "public"

    </filesMatch>

    <filesMatch "\.(js)$">

        Header set Cache-Control "private"

    </filesMatch>

    <filesMatch "\.(x?html?|php)$">

        Header set Cache-Control "private, must-revalidate"

    </filesMatch>

</ifModule>

# END Cache-Control Headers

Please note:

  • The mod_expires module controls the Expires header, setting the duration for cached files.
  • The mod_headers module manages Cache-Control headers for different file types.
  • There’s no need to install a max-age directive with Cache-Control, as it’s included with the mod_expires module.
  • “must-revalidate” means the browser should recheck the file as it ages, but not necessarily on every request.

Step 4: Reduce MySQL Database Size for Faster Loading

Starting from WordPress 2.6, WordPress automatically saves versions of entries in the MySQL database. Even after saving a post, these versions linger in the database, contributing to its size and slowing down the site. To improve site speed, consider disabling the saving of multiple versions.

Paste the following code in the wp-config.php file to turn off post revisions:

define('WP_POST_REVISIONS', false);

If you prefer to keep this feature but limit the database to store versions for a specific number of days (e.g., 10 days), you can modify the code accordingly.

Adding an expires header won’t impact download speed on the first visit, but it significantly reduces download time for subsequent views or returning users.

To observe your site’s performance metrics, including the number of requests and processing time, insert the following code after the copyright text in the footer.php file of your theme:

<?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds.

Final Say

In 2024, enhancing the speed of your WordPress website remains crucial for providing a seamless user experience. Leveraging the power of .htaccess, you can implement various optimizations to boost loading times significantly . From Gzip compression to browser caching and disabling unnecessary features like ETags, these techniques can have a substantial impact on your site’s performance. Additionally, making informed decisions about MySQL database size by managing post revisions contributes to an overall faster and more efficient WordPress site.

FAQs on Speed up WordPress Using .htaccess

1. Why is Gzip compression important for speeding up a WordPress site?

Gzip compression reduces the size of files sent over the internet, leading to faster loading times. It is particularly effective for compressing HTML documents, scripts, and style sheets. By implementing Gzip compression through .htaccess, you can optimize your site’s performance and save bandwidth.

2. Why should I consider disabling ETags in WordPress using .htaccess?

ETags, or Entity Tags, are mechanisms used by web servers and browsers to determine if a cached component matches the original. Disabling ETags through .htaccess can be beneficial for performance as it simplifies the process of checking file relevance. This can lead to improved loading times by avoiding unnecessary requests to the server, ultimately enhancing the efficiency of your WordPress site.

Want faster WordPress?

WordPress Speed Optimization

Try our AWS powered WordPress hosting for free and see the difference for yourself.

No Credit Card Required.

Whitelabel Web Hosting Portal Demo

Launching WordPress on AWS takes just one minute with Nestify.

Launching WooCommerce on AWS takes just one minute with Nestify.