.htaccess Generator
Build Apache .htaccess redirects, caching and security rules with conflict checks
Errors: 0 · Warnings: 0 · Redirects: 0
Related Tools
What it generates
The generator writes an Apache 2.4 .htaccess file for common redirect, performance and security needs: force HTTPS, one canonical host, trailing-slash and index.html clean-up, custom 301/302/307/308 redirects, gzip compression, Expires caching presets, directory-listing and dotfile protection, basic security headers, optional HSTS and custom error pages. Every section has an inline comment, and the order is always the same so diffs stay readable.
Conflict checks
Before a redirect is written, it is checked for malformed sources, unsafe or unexpected destination protocols, duplicates, self-redirects, A to B to A loops and chains that cost an extra hop. Rules that would loop or never run are left out of the file and listed under Findings. Canonical host and trailing-slash options are single choices, so contradictory rules such as add www and remove www cannot be combined.
Apache details that matter
All rewrite rules share one IfModule mod_rewrite block with a single RewriteEngine On. HTTPS and host changes happen in one redirect, trailing-slash rules skip real files and folders, and slash or index redirects apply only to GET and HEAD requests. Access control uses Require all denied, never the obsolete Order and Deny syntax. Module-dependent blocks are wrapped in IfModule, so a missing module skips a block silently instead of breaking the site.
Before you deploy
.htaccess works only where the server AllowOverride setting allows these directives; otherwise Apache answers 500. Place the file in the document root, because custom redirect paths are relative to it. Behind a proxy or CDN that terminates TLS, the HTTPS rule cannot see the original scheme and may loop. Enable HSTS only after HTTPS works everywhere, starting with a short max-age. Browsers save the download as htaccess.txt; rename it to .htaccess on the server. Everything runs in your browser and nothing is uploaded.
Frequently asked questions
- Why does my site return 500 after uploading .htaccess?
- Apache answers 500 when the file contains a directive that the server AllowOverride setting does not allow. Options lines need AllowOverride Options, Require needs AuthConfig, rewrite, header and error-document directives need FileInfo, and Expires needs Indexes. The generator lists exactly which categories your file needs, so you can ask your host or turn that section off.
- Why does Force HTTPS loop behind Cloudflare or a load balancer?
- When a proxy or CDN terminates TLS and talks to Apache over plain HTTP, the HTTPS server variable is always off, so the rule redirects every request again. Use the HTTPS setting of your host or CDN, or a rule that checks the X-Forwarded-Proto header for your specific setup, instead of the plain HTTPS check.
- Is it safe to enable HSTS in .htaccess?
- Only once HTTPS works on every page and, with includeSubDomains, on every subdomain. Browsers remember the policy for max-age seconds and refuse plain HTTP until it expires, so a mistake cannot be undone quickly. HSTS is off by default here; start with a five-minute max-age and raise it gradually.
