Website Speed

Reduce Page Load Time with .htaccess

On compatible Apache hosting, a carefully managed .htaccess file can support faster delivery through compression and sensible browser caching.

By Adrian IscruPublished 4 September 2020Reviewed 25 June 20267 min read

A .htaccess file can be useful for website performance, but it is not a shortcut and it is not suitable for every server. Used carefully on compatible Apache hosting, it can help browsers reuse files and reduce the size of text-based responses.

For WordPress websites, a plugin or host-level setting may be the right answer in some cases. Editing a configuration file directly, however, can avoid adding another plugin when you understand the change you are making and can test it safely.

This guide updates an article first published in 2020. It focuses on the practical performance features that are commonly relevant: compression, browser caching and a careful workflow that protects the live site.

1. Check compatibility before editing

The .htaccess file is used by Apache and some compatible web-server environments. It is not read by Nginx in the same way, and many managed hosting platforms provide equivalent controls through a dashboard, server configuration or content delivery network instead. Before copying any rule, check your host’s documentation and find out which stack your website uses.

It is also important to know whether your hosting account permits the directives you plan to use. Some hosts disable or limit modules such as mod_deflate, mod_brotli or mod_expires. A rule that works perfectly elsewhere may create an error on a different server.

Important: Do not add .htaccess rules because they appeared in a generic speed guide. Server configuration is context-specific. Confirm compatibility, keep a working backup and make one controlled change at a time.

2. Back up and access the file safely

The .htaccess file is normally found in the website’s root directory, often alongside folders such as wp-admin, wp-content and wp-includes on a WordPress installation. Because the name begins with a full stop, it may be hidden in a file manager or FTP client until hidden files are enabled.

You can usually access it through a hosting control panel, a secure file manager, SFTP or an FTP client such as FileZilla. Before editing, download a local copy or duplicate the file with a clear backup name. If your host provides a staging environment, test there first.

  • Save a copy of the existing file before changing anything.
  • Use a plain-text editor that will not add formatting characters.
  • Change one section at a time and record what you changed.
  • Keep a second browser window open so you can test the public site immediately.

3. Enable compression where supported

Compression can reduce the amount of data transferred for text-based resources such as HTML, CSS, JavaScript, JSON, XML and SVG. The server compresses an eligible response before it is sent; the browser then decompresses it automatically. This can make pages quicker to deliver, particularly when they include substantial text or code.

Modern platforms may use Brotli, Gzip or both. Brotli is often enabled at a host or CDN level, while Gzip is widely supported by Apache installations. Do not attempt to compress files that are already compressed, such as JPEG, WebP, AVIF, ZIP files or most video formats; doing so usually adds processing without a useful gain.

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

This is an illustrative Apache configuration pattern rather than a universal copy-and-paste solution. Your host may provide a preferred version, may already enable compression globally, or may require a different configuration approach.

4. Set sensible browser caching rules

Browser caching allows a returning visitor’s browser to reuse files it has already downloaded. Rather than requesting the same logo, stylesheet, script or font on every page view, the browser can keep a local copy for the period defined by the cache policy. This reduces repeat transfers and can make subsequent visits feel much faster.

Static assets often suit longer cache periods because they do not change often. HTML pages and frequently changing content need more careful handling. If you set a very long cache duration but update a file without changing its name or version, visitors may continue to see an older copy.

<IfModule mod_expires.c> ExpiresActive On ExpiresByType text/css "access plus 1 month" ExpiresByType application/javascript "access plus 1 month" ExpiresByType image/jpeg "access plus 6 months" ExpiresByType image/png "access plus 6 months" ExpiresByType image/webp "access plus 6 months" ExpiresByType image/svg+xml "access plus 6 months" </IfModule>

These values are examples, not fixed rules. A good caching policy reflects how you publish and update your site. Versioned assets, such as style.abc123.css, are easier to cache aggressively because a changed file receives a new URL.

Do not cache your way around a problem

Caching improves repeat delivery, but it does not fix oversized images, inefficient database queries, unnecessary plugins or a slow server response. Treat it as one part of a wider performance plan.

5. Test changes and avoid common mistakes

After every edit, test the homepage, an important service page, contact forms, login areas and any checkout or booking journey. Check the site in a private browser window as well as your normal browser, because an existing cache can hide a problem or make a change appear to work when it has not.

If the site returns a 500 Internal Server Error after an edit, restore the backup file first. This usually points to unsupported syntax, a missing module or a formatting problem. Do not continue adding rules until the original site is working again.

Performance testing tools can help verify whether compression and cache headers are present, but they should sit alongside real-world checks on desktop and mobile. The goal is a website that feels faster to visitors, not just a configuration file with more lines in it.

When to use another approach

For many businesses, host-level caching, a content delivery network or a reliable performance plugin may be safer and easier to maintain than editing .htaccess directly. If your site uses Nginx, a managed WordPress platform or a hosting dashboard with its own optimisation controls, follow the method recommended by that environment.

If you are unsure which changes are worthwhile, start with a Website Audit. For a broader review of images, hosting, plugins and caching, see 5 Practical Ways to Improve Website Speed. You can also explore Website Performance or Fix & Optimisation for hands-on support.

About the author: Adrian Iscru is a web developer, SEO specialist and lecturer working with UK small businesses on website audits, performance, WordPress care and practical digital improvements.
Frequently asked questions

.htaccess performance FAQs

What is a .htaccess file?

A .htaccess file is a per-directory configuration file used by Apache-compatible web servers. It can control settings such as redirects, access rules, caching headers and compression where the hosting environment allows those directives.

Will .htaccess work on every hosting platform?

No. .htaccess is associated with Apache and some compatible server setups. It is not used by Nginx in the same way, and some managed hosts restrict or replace these settings. Check the hosting documentation before making changes.

Is Brotli better than Gzip?

Brotli can often provide more efficient compression for supported browsers, especially for text-based resources. Whether it is available depends on the web server, CDN or hosting platform. Gzip remains widely supported and useful where Brotli is not enabled.

Can editing .htaccess break a website?

Yes. A syntax error or conflicting directive can trigger server errors, redirects or unexpected behaviour. Always take a backup, make one controlled change at a time and test important pages immediately after editing.

Not sure what is slowing your website?

Start with a practical website audit.

Get a clear view of performance, mobile usability, technical SEO, trust and conversion issues — with priorities you can act on.