.htaccess Generator

Apache .htaccess rules for redirects and security

Ad placeholder (leaderboard)

Apache config without the guesswork

The .htaccess Generator builds the Apache directives most sites need — forcing HTTPS, choosing www or non-www, enabling compression and caching, and adding security headers. Hand-written .htaccess rules are unforgiving: one typo returns a 500 error for every visitor. Toggle the rules you want and copy a clean, IfModule-guarded config.

How it works

.htaccess is a per-directory configuration file Apache reads on each request. Redirects use the mod_rewrite engine, which matches conditions and rewrites or redirects the URL:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]

RewriteCond checks a condition (here, that the request is not already HTTPS) and RewriteRule performs a permanent (R=301) redirect. Compression (mod_deflate), caching (mod_expires) and header rules (mod_headers) are wrapped in <IfModule> blocks so they are silently skipped when a module is absent — protecting you from accidental 500s.

Tips and notes

  • Pick one canonical host (either www or non-www) and redirect the other to avoid duplicate-content SEO issues.
  • Always back up your existing .htaccess before replacing it; a single bad line takes the whole directory down.
  • These rules are Apache-only. On Nginx, translate them into server block directives instead.
Ad placeholder (rectangle)