.htaccess Generator

Redirects, HTTPS enforcement and cache headers, assembled into one Apache config.

Start from a stack that already works
Blocks to include

Each of these is a separate block in the file, and each one is wrapped in its own <IfModule> guard.

Redirects

The file — htaccess.txt

Before you paste this on a server

  1. Keep a copy of the file you are replacing. A syntax error here is a 500 on every request, including the admin page you would fix it from.
  2. Check AllowOverride. If the vhost says AllowOverride None, Apache never reads the file and nothing you add will do anything.
  3. Load one URL that should redirect and one that should not, and read the status code rather than the rendered page.
  4. Count the hops: curl -sIL and look for exactly one 3xx before the 200. Two means you have chained a rule onto an existing one.
  5. On nginx, none of this applies. The same rules belong in the server block, and the file will be ignored silently.

How the file is put together

Build an .htaccess file from readable rules: redirects, HTTPS enforcement, gzip compression and browser cache headers. Review the output before you deploy it. Nothing leaves your browser.

Why every block is wrapped in IfModule

Apache aborts the whole request with a 500 when it meets a directive from a module that is not loaded. Wrapping each block in <IfModule> means a host without mod_expires simply skips the cache headers instead of taking the site down, which is the difference between a missing optimization and an outage.

The redirects all go through mod_rewrite rather than mod_alias Redirect directives. Mixing the two is a classic source of confusion: they run in different phases, so the file stops executing in the order you read it.

Questions this file raises

Why is my .htaccess file being ignored?

Apache only reads it when AllowOverride is enabled for that directory, and nginx never reads it at all. On nginx the equivalent rules belong in the server block.

Should I use a 301 or a 302 redirect?

Use 301 when the move is permanent - it passes ranking signals and browsers cache it. Use 302 for temporary changes such as a maintenance page or a short campaign.

Does .htaccess slow a site down?

Slightly: Apache re-reads it on every request for every directory in the path. On a server you control, move the same rules into the vhost config instead.

Everything happens in this tab. Read the output before it goes near a server: a mistake in this file is a 500 on every request, not a broken corner of one page.