.htaccess Generator

Build Apache rules for https, www, caching and compression.

SEO Tools Free, no sign-up Runs in your browser

.htaccess Generator tool

Rules to include

A syntax error in .htaccess returns a 500 error for the whole directory rather than being ignored. Keep a copy of your working file before replacing it.

About the .htaccess Generator

The .htaccess file configures Apache on a per-directory basis, and most shared hosting gives you that file as the only server configuration you control. This generator assembles the rules people actually need, each as a block you can switch on or off, with a comment explaining what it does.

The common blocks are here: force https, choose between www and non-www as the canonical host, set far-future cache headers on static assets, enable gzip or brotli compression, point at custom error pages, block directory listing, protect sensitive files, and add the basic security headers.

One warning worth taking seriously. A syntax error in .htaccess does not degrade gracefully; it returns a 500 error for the whole directory and everything under it. Always keep a copy of the working file before replacing it, and test on staging if you have one. If you control the main server configuration, putting these rules there instead is faster, because .htaccess is re-read on every request.

How to use the .htaccess Generator

  1. Pick the rules you needEach block is independent and explained.
  2. Set your domainUsed by the https and www redirect rules.
  3. Review the generated fileEvery block carries a comment saying what it does.
  4. Back up, upload, testKeep the old file. A syntax error returns a 500 for the whole directory.

Features

  • Force HTTPS with a permanent redirect
  • Canonical www or non-www
  • Browser caching with sensible per-type lifetimes
  • Gzip and brotli compression
  • Custom 404 and 500 error pages
  • Directory listing disabled and sensitive files protected
  • Basic security headers
  • Commented output you can read and edit

Who uses this tool

The .htaccess generator is used by writers, developers, students, marketers and anyone else who needs the job done once without installing software. Common cases include:

  • Forcing https on a site that still answers on http
  • Fixing a site reachable at both www and non-www
  • Adding cache headers to speed up repeat visits
  • Turning on compression on shared hosting
  • Pointing errors at a custom 404 page

Frequently asked questions

What happens if I make a syntax mistake?

Apache returns a 500 error for that directory and everything below it, so the site goes down rather than ignoring the bad line. Always keep a copy of the working file before you replace it.

Should I use .htaccess or the main server config?

The main configuration is faster, because .htaccess is read on every single request. Use .htaccess when you do not control the server, which is the situation on most shared hosting.

www or non-www, which is better?

Neither is better for search. What matters is picking one and redirecting the other, so that search engines and analytics see a single canonical host rather than two versions of every page.

Does this work on nginx?

No. The syntax is Apache-specific and nginx does not read .htaccess at all. Use the redirect generator's nginx output and put caching and compression directives in your server block.

How long should static assets be cached?

A year is normal for files with a hash in the name, since a change produces a new filename. For assets whose name never changes, a shorter lifetime avoids serving stale files to returning visitors.

Will these rules speed up my site?

Caching and compression genuinely help, often substantially on a site serving uncompressed assets. They are not a substitute for reducing how much you send in the first place.

If the .htaccess generator is not quite what you need, these other free tools solve closely related problems.

301 Redirect Generator

Generate redirect rules for Apache, nginx, Netlify, Vercel and Cloudflare from a mapping.

SEO Tools Use tool

Robots.txt Generator

Generate robots.txt with per-crawler rules, sitemap lines and common presets.

SEO Tools Use tool

CSP Generator

Generate a Content Security Policy header directive by directive, with explanations.

Developer Tools Use tool