.htaccess generator

Build an Apache or LiteSpeed .htaccess file by ticking boxes: HTTPS and www in a single redirect, browser caching and compression, security headers, WordPress hardening, blocked IPs and bots, and your own redirects. Every line comes with a comment saying what it does, and the blocks are in the order Apache needs them.

Domain and HTTPS

www
Trailing slash

Platform

Speed

Security headers

Protection

Redirects and error pages

Lots of redirects? The redirect map generator builds them from your old and new sitemaps.

.htaccess · 91 lines
Server options
# Stop Apache listing the files in folders without an index page
Options -Indexes
# Don't advertise the Apache version on error pages
ServerSignature Off
Protect sensitive files
# Block hidden files and folders like .env, .git and .htpasswd
<FilesMatch "^\.">
    Require all denied
</FilesMatch>
RedirectMatch 404 /\.(git|svn|env|hg)(/|$)

# Block files that should never be downloaded
<FilesMatch "(?i)(composer\.(json|lock)|package(-lock)?\.json|\.(sql|bak|log|ini|sh|swp)|~)$">
    Require all denied
</FilesMatch>

# Protect wp-config.php
<Files wp-config.php>
    Require all denied
</Files>

# Block xmlrpc.php, a common target for password-guessing attacks (breaks Jetpack and the old mobile app)
<Files xmlrpc.php>
    Require all denied
</Files>
Redirects and access rules
<IfModule mod_rewrite.c>
RewriteEngine On

# HTTPS and no www in one redirect, so there's never a chain
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.co.uk%{REQUEST_URI} [L,R=301]
</IfModule>
Security and caching headers
<IfModule mod_headers.c>
    # Tell browsers to only ever use HTTPS
    Header always set Strict-Transport-Security "max-age=31536000"
    # Stop browsers guessing file types
    Header always set X-Content-Type-Options "nosniff"
    # Stop other sites showing yours in a frame (clickjacking)
    Header always set X-Frame-Options "SAMEORIGIN"
    # Send only the domain, not the full URL, to other sites
    Header always set Referrer-Policy "strict-origin-when-cross-origin"

    # Cache static files in the browser
    <FilesMatch "\.(jpe?g|png|gif|webp|avif|svg|ico|woff2?)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>
    <FilesMatch "\.(css|js)$">
        Header set Cache-Control "public, max-age=2592000"
    </FilesMatch>
</IfModule>
Browser caching
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 hour"
    ExpiresByType text/html "access plus 0 seconds"
    ExpiresByType image/jpeg "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"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
</IfModule>
Compression
# Compress text files before sending them (usually 60 to 80% smaller)
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript application/javascript application/json application/xml application/rss+xml image/svg+xml font/ttf font/otf
</IfModule>
WordPress
# BEGIN WordPress
# WordPress rewrites this block, so keep your own rules above it
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
  • If the site sits behind a load balancer or a CDN that talks to the server over HTTP (Cloudflare Flexible SSL, some Cloudways and AWS setups), tick "Behind a proxy" or the HTTPS redirect will loop.
  • Blocking xmlrpc.php breaks Jetpack and some old apps. Leave it off if the client uses them.
  • Back up the current .htaccess before replacing it. A single typo can take the whole site down with a 500 error, and the backup is how you get it back.

What .htaccess does

.htaccess is a configuration file Apache (and LiteSpeed, which most cheap UK hosting uses) reads on every request. It sits in the website's root folder and controls redirects, caching, security headers and who can access what, without needing access to the main server config. That's why WordPress, cPanel hosting and most shared hosts rely on it.

It doesn't work on nginx. If the site is on nginx (common on Cloudways, Kinsta, WP Engine and most VPS setups), these rules go in the server config instead, and your host may handle HTTPS and caching for you already. The stack checker will tell you which server a site runs on.

The order matters

Apache reads the file top to bottom. Redirects go before WordPress's own block, which sends everything else to index.php and would swallow anything after it. HTTPS and www are combined into a single rule so visitors are redirected once, not twice. Headers and caching can go anywhere, but keeping them together makes the file easier to maintain. The generator puts everything in that order.

Questions agencies ask

Turn on Force HTTPS above. It adds a RewriteCond that checks whether the request came in over HTTPS and a RewriteRule that sends anything else to the https:// version with a 301. If the site is behind a proxy or load balancer, tick that option too, so it checks the X-Forwarded-Proto header instead and avoids a redirect loop.

FOR AGENCIES

Server config is not your team's favourite job?

Redirect loops, 500 errors after an .htaccess change, slow sites with no caching and missing security headers are all regular fixes for me on agency client sites, on Apache, LiteSpeed and nginx.

  • White-label, under your brand, or alongside your team
  • Laravel, WordPress, WooCommerce, integrations and tracking
  • Inherited and half-finished sites taken over and stabilised
  • UK based, straight to the developer, no account managers

Related free tools