.htaccess Generator for Apache 2.4
Tick boxes, get a correct .htaccess: 301 redirects, HTTPS + www force, error pages, caching, gzip, htpasswd, IP rules, hotlink protection, PHP flags.
What you get that a snippet site doesn’t give you
Most .htaccess snippets floating around the web are Apache 2.2-era, half of them syntax-error on a modern server, and almost none say why the lines are shaped that way. This generator emits Apache 2.4 directives with the guards and ordering already correct: every feature sits in the <IfModule> block for the module it needs, so a host without mod_php or mod_expires degrades to “feature off” instead of a 500 error page. Comments inside the output explain each block and flag the traps — proxy redirect loops, htpasswd paths that leak, the Require not ip rule that denies everybody if you write it wrong.
The pieces, honestly scoped
- Redirects — “exact page” rows emit anchored
RedirectMatchpatterns, because plainRedirect 301 /old /newis a prefix match that also catches/old.bakand/oldfoo. “Whole section” rows carry/dirand everything under it to the new prefix without over-matching lookalike paths. More in the 301 redirects guide. - HTTPS + canonical host — combined into a single-redirect rule when both are on (one hop, not two), with the
X-Forwarded-Protocaveat for CDN-fronted sites. See force HTTPS and pick one host. - Access control — allow-lists use
<RequireAny>(first match wins); deny-lists use<RequireAll>withRequire all granted+Require not ip, because a bareRequire not ipcan never grant access and would 403 your whole site. The password & IP guide covers htpasswd creation and combining auth with IP rules. - Performance —
mod_deflatefor text types only (JPEG/PNG/WOFF2 are already compressed and are deliberately excluded),mod_expiresTTLs with an immutableCache-Controloption for fingerprinted assets. Caching & compression explains the trade-offs. - Debugging — if the file 500s, the 500-error guide has the ordered checklist; if you want to understand what
RewriteCondactually does, start with mod_rewrite basics.
Everything is assembled in your browser — the file never crosses the network until you upload it to your server.
Frequently asked questions
Where does the generated file go?
Save it as .htaccess (a dotfile, no extension) in your site's document root — the directory your domain serves, often public_html/, htdocs/ or www/. Rules apply to that directory and everything under it. On shared hosting you usually upload it with FTP or the file manager; on your own server you may prefer putting the same directives in the VirtualHost config instead — .htaccess is re-read on every request, the main config is not.
The site started returning 500 after I uploaded the file. Now what?
A 500 right after upload means the file is being read but a directive inside is failing. The usual causes: your host doesn't allow Options overrides, a module the rules need isn't loaded (our <IfModule> guards prevent most of these), a typo introduced during paste, or AllowOverride is too restrictive. Delete the file to confirm, then re-add sections one at a time. The 500-error debugging guide walks the whole checklist.
Why wrap everything in <IfModule> blocks?
Because a directive for a module that isn't loaded kills the whole site with a 500 — php_value on a PHP-FPM host is the classic example. The guard makes the block simply not apply when its module is absent: the feature is off instead of the site being down. Trade-off, stated honestly: a skipped guard means the feature silently doesn't work, so always verify the behavior after uploading.
301 or 302 — and does the generator's ordering matter?
Use 301 (permanent) for moved content — search engines transfer ranking signals and browsers cache it. 302/307 are for temporary moves. Inside the file, mod_rewrite rules run before mod_alias ones regardless of order, so the canonical-host/HTTPS block always fires first; we emit it first anyway because humans read top-down.
Does this work on Nginx, LiteSpeed or Cloudflare?
Nginx ignores .htaccess entirely — you'd translate the rules into nginx.conf (return 301, location blocks). LiteSpeed reads .htaccess for rewrites but not every directive. Cloudflare can do redirects and HTTPS at the edge before the request reaches Apache, which is often the better place. The output here targets real Apache 2.4 with standard modules.
Is it safe to paste these rules into WordPress's .htaccess?
Mostly yes — the rules here play fine alongside WordPress's own # BEGIN WordPress block (keep that block; our WordPress front controller toggle is for sites without it). Put canonicalization/redirects before it. Never edit inside the BEGIN/END markers — WordPress rewrites that section.
Why does the htpasswd path have to be absolute and outside the web root?
AuthUserFile takes a filesystem path, not a URL — a relative one resolves against ServerRoot and just won't find your file. Outside the web root matters because a .htpasswd inside public_html can be downloaded by anyone: it holds your password hashes. Generate it with htpasswd -c -B /home/you/.htpasswd user. More in the password protection guide.
Does anything get uploaded when I use this page?
No. The file is assembled by JavaScript in your browser tab — check the network panel and you'll see zero requests after load. Your domain, IPs and paths never leave the device.
Latest articles
How mod_rewrite Actually Works in .htaccess
The mental model for .htaccess rewrites: rule anatomy, cond pairing, per-dir path handling and the flags that matter.
301 Redirects in .htaccess: the Correct Forms
Which directive for which redirect, why `Redirect` over-matches, and how to keep 301s from chaining or looping.
Force HTTPS and a Single Canonical Host
One rule to canonicalize scheme + host in a single hop — plus the proxy loop fix and when HSTS belongs on top.
.htaccess 500 Errors: Every Cause and the Fix
The systematic way to find which line 500s your site — and why your rules might be silently ignored instead.
Password-Protect a Directory with .htpasswd
Basic auth done correctly, IP allow/deny lists that don't 403 everyone, and how to combine them for staging lockdowns.
Caching and Compression via .htaccess
The two cheapest speed wins on Apache: send fewer bytes and let the browser keep them. Correct directives, honest trade-offs.