Skip to content

You are here: Home / kb / How to Improve Website Speed Using .htaccess

Knowledgebase Guide

Improve Website Speed Using .htaccess

The .htaccess file can be used to improve website performance by enabling
browser caching and compression for static files such as images, CSS and JavaScript.

This guide explains some safe performance settings that can be added to your website's .htaccess file.

Important: Always make a backup of your existing .htaccess file before making changes.
An incorrect directive can cause your website to display a 500 Internal Server Error.

Where Is the .htaccess File?

For most websites, the main .htaccess file is located inside the website's document root.

For a primary cPanel website this is commonly:

public_html/.htaccess

For WordPress installations, it is normally in the same directory as:

wp-admin
wp-content
wp-includes
wp-config.php

You can edit the file using File Manager in cPanel or DirectAdmin.

Tip: Files beginning with a dot are hidden files. If you cannot see
.htaccess, enable the option to show hidden files in your File Manager.

1. Enable Browser Caching

Browser caching tells a visitor's web browser that certain files can be stored locally for a period of time.

This means that when someone visits another page on your website, their browser may not need to download the same
logo, CSS file, JavaScript file or font again.

Add the following to your .htaccess file:

<IfModule mod_expires.c>
ExpiresActive On

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

# CSS and JavaScript
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"

# Fonts
ExpiresByType font/woff "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
ExpiresByType application/font-woff "access plus 1 year"

# Other static files
ExpiresByType application/pdf "access plus 1 month"

ExpiresDefault "access plus 1 month"
</IfModule>

This allows browsers to retain frequently used static files rather than downloading them again on every page visit.

Why Are Images Cached for Longer?

Images, fonts and similar files usually change less frequently than page content.

For this reason, they can normally be cached for longer periods.

CSS and JavaScript are shown with a shorter cache period in this example because these files may be changed more frequently
when your website, theme or plugins are updated.

Tip: WordPress and many modern caching systems automatically change or version static file
URLs when files are updated. This makes longer browser caching periods safer.

2. Enable Compression

Compression reduces the amount of data that must be transferred between the web server and the visitor's browser.

Text-based files such as HTML, CSS, JavaScript and XML can often be compressed significantly.

Add:

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>
Note: Compression may already be enabled by the web server. Adding these rules does not
necessarily make a website faster if compression is already active.

3. Browser Caching and Compression Together

You can use both sets of rules together.

For example:

# Browser caching
<IfModule mod_expires.c>
ExpiresActive On

ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/avif "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"

ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"

ExpiresByType font/woff "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"

ExpiresDefault "access plus 1 month"
</IfModule>

# Compression
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>

Where Should I Add the Code in WordPress?

A standard WordPress .htaccess file normally contains a section similar to:

# BEGIN WordPress

...

# END WordPress

Add your custom performance rules outside the WordPress-generated section.

For example:

# Cheap Host performance settings

<IfModule mod_expires.c>
...
</IfModule>

<IfModule mod_deflate.c>
...
</IfModule>

# BEGIN WordPress
...
# END WordPress
Important: Do not normally place your custom settings between
# BEGIN WordPress and # END WordPress. WordPress may rewrite this section and remove your changes.

Does LSAPI Pro Affect These Settings?

Cheap Host uses LSAPI Pro for PHP processing on supported hosting accounts.

LSAPI Pro is designed to improve the way PHP applications are handled by the server, but it is separate from
full-page website caching.

The .htaccess settings in this guide can still be used to improve the delivery of static content such as:

  • Images.
  • CSS files.
  • JavaScript files.
  • Fonts.
  • Other cacheable static resources.

Browser caching and compression can therefore still provide a performance benefit even when PHP is already being handled through LSAPI Pro.

Tip: LSAPI Pro helps with PHP processing performance, while the browser caching and compression
rules in this guide help reduce the amount of static content that needs to be transferred to repeat visitors.

Do I Need a WordPress Caching Plugin?

The settings above mainly improve:

  • Browser caching.
  • Compression.
  • Delivery of static files.

A WordPress caching plugin can provide additional optimisation such as:

  • Full-page caching.
  • CSS and JavaScript optimisation.
  • Image optimisation.
  • Lazy loading.
  • Database optimisation.
  • Cache purging when pages or posts are updated.

If you use a WordPress caching plugin, make sure it is compatible with your website and avoid enabling duplicate optimisation
features in several plugins at the same time.

Do Not Add Every Speed Rule You Find Online

There are many old .htaccess optimisation examples available online.

Some contain directives that are no longer useful, while others may conflict with your web server, WordPress caching
plugin or existing website configuration.

In particular, avoid blindly adding:

  • Large collections of rewrite rules you do not understand.
  • Server caching rules intended for a different hosting platform.
  • Rules that cache every URL on a dynamic website.
  • Rules copied from another website's .htaccess file.
Recommended: Start with browser caching and compression. For WordPress full-page caching,
use a WordPress-aware caching solution rather than manually caching every page through .htaccess.

Testing Your Website After Editing .htaccess

After saving the file:

  1. Open your website in a new browser tab.
  2. Check the homepage and several internal pages.
  3. Check the WordPress administration area if you use WordPress.
  4. Check important features such as contact forms or shopping carts.
  5. Clear any existing website cache before testing performance changes.

You can also use website performance testing tools to compare the site before and after the changes.

What If My Website Shows a 500 Error?

If your website displays a 500 Internal Server Error immediately after editing .htaccess:

  1. Open File Manager.
  2. Edit your .htaccess file.
  3. Remove the code you just added or restore your backup copy.
  4. Save the file.
  5. Reload your website.

Because the examples in this guide use <IfModule> checks, unsupported modules should normally be skipped
rather than causing an error. However, keeping a backup is still strongly recommended.

Other Ways to Improve Website Speed

Changing .htaccess is only one part of website optimisation.

For the best results:

  • Optimise large images before uploading them.
  • Use WebP or AVIF images where appropriate.
  • Remove unused WordPress plugins and themes.
  • Keep WordPress, plugins and themes updated.
  • Use an appropriate caching solution.
  • Keep your website on a supported PHP version.
  • Avoid unnecessarily large scripts, fonts and third-party resources.

Still Having Problems?

If you are having problems changing your .htaccess file or would like help checking your website's
performance settings, please sign in to your hosting account and open a support ticket.

Our team will be happy to help.

Open Support Ticket