htaccess generator
Build Apache .htaccess configuration from a library of common directives.
# Generated .htaccess — ToolChamp# Force HTTPSRewriteEngine OnRewriteCond %{HTTPS} offRewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]# Browser caching (30 days default)<IfModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 30 days" ExpiresByType text/css "access plus 1 year" ExpiresByType application/javascript "access plus 1 year" ExpiresByType image/jpeg "access plus 1 year" ExpiresByType image/png "access plus 1 year" ExpiresByType image/svg+xml "access plus 1 year" ExpiresByType font/woff2 "access plus 1 year"</IfModule># Security headers<IfModule mod_headers.c> Header set X-Content-Type-Options "nosniff" Header set X-Frame-Options "SAMEORIGIN" Header set X-XSS-Protection "1; mode=block" Header set Referrer-Policy "strict-origin-when-cross-origin" Header set Permissions-Policy "camera=(), microphone=(), geolocation=()"</IfModule>Nine of the directive blocks that end up in almost every Apache .htaccess file are here as checkboxes, and the file assembles itself in the panel beside them, syntax-highlighted and commented, as you tick. Three are on when the page loads — force HTTPS, security headers and browser caching — because those are the three most sites are missing. Four of the blocks open a small settings field when enabled: the www preference on the HTTPS redirect, the default cache duration in days, the domain to allow for hotlink protection, and the paths of your 404 and 500 pages. Anything the presets do not cover goes in as a named custom block, which is appended verbatim with its own comment header. The result copies to the clipboard or downloads as a file already called .htaccess.
Key facts about htaccess generator
| Directive blocks offered | 9 — force HTTPS, gzip compression, browser caching, HSTS, security headers, hotlink protection, SPA fallback, custom error pages and blocking sensitive files |
|---|---|
| Enabled by default | Force HTTPS, security headers and browser caching |
| HTTPS rule | RewriteCond on %{HTTPS} off with a 301 RewriteRule, plus an optional second condition that also forces the www prefix |
| Security headers written | X-Content-Type-Options nosniff, X-Frame-Options SAMEORIGIN, X-XSS-Protection, Referrer-Policy strict-origin-when-cross-origin, and a Permissions-Policy that denies camera, microphone and geolocation |
| HSTS value | max-age=31536000 with includeSubDomains — one year, no preload directive |
| Caching block | mod_expires with your chosen default in days, plus one-year overrides for CSS, JavaScript, JPEG, PNG, SVG and WOFF2 |
| Gzip block | mod_deflate on HTML, plain text, CSS, JavaScript, JSON, XML and SVG |
| Hotlink protection | Blocks jpg, jpeg, png, gif, svg and webp requests whose referer is neither empty nor your domain, returning 403 |
| Blocked file patterns | A FilesMatch covering .htaccess, .htpasswd, .env and .git |
| Module guards | The gzip, caching, HSTS and header blocks are wrapped in IfModule, so the file does not 500 on a server without mod_deflate, mod_expires or mod_headers |
| Custom rules | Any number of named free-text blocks, appended in order with a comment header each |
| Not generated | Per-URL 301 redirects, password protection with AuthUserFile, IP allow and deny lists, and PHP value overrides — write those as custom rules |
| Preview visibility | The highlighted preview panel is shown on wide screens only; on a phone you get the copy and download buttons instead |
What happens to your file
There is no server in this loop. Each block is a template string in the page bundle; ticking a box concatenates it into the output, and the settings fields are substituted straight into the text. The file you download is built from a Blob made in the tab and handed to a temporary link, so the bytes never travel anywhere. Nothing you type — your domain name for hotlink protection, your error page paths, any custom rule you paste in — is transmitted, logged or stored, and a page refresh resets everything to the three default blocks. The page keeps working offline once loaded.
About this tool
- 1
Start with the three defaults
Force HTTPS, security headers and browser caching are already ticked. If you only came for the basics, the file in the preview is finished and you can copy it now.
- 2
Tick what else you need
Add gzip if compression is not already handled upstream, SPA fallback if a single-page app needs every path routed to index.html, and blocking of sensitive files if .env or .git might be reachable.
- 3
Fill the settings that appear
Enabling hotlink protection asks for the domain to allow. Caching asks for a default duration in days. Custom error pages ask for your 404 and 500 paths. HTTPS asks whether www should be forced too.
- 4
Add anything missing as a custom rule
Use Add custom rule for redirects of individual URLs, password protection or anything else the checkboxes do not cover. Give it a name — it becomes the comment above the block.
- 5
Read the preview before you trust it
Every block carries a comment saying what it does. A rewrite rule you cannot explain is a rewrite rule you should not deploy, because .htaccess errors take the whole site down with a 500.
- 6
Upload and verify
Download the file, put it in your document root, then load the site in a private window. Check that http redirects to https once and not in a loop, and that the headers you asked for appear in the network panel.
| Target server | Apache HTTP Server 2.4, with mod_rewrite, mod_headers, mod_expires and mod_deflate where those blocks are used |
|---|---|
| Prerequisite | AllowOverride must permit the directives — on a shared host it usually does; on your own server the default in many distributions is AllowOverride None |
| Not for | Nginx, Caddy, IIS and LiteSpeed in its native configuration — .htaccess is an Apache mechanism |
| Output | Plain text with comment headers per block, starting with a generated-by line |
| Download file name | .htaccess, with a leading dot — some operating systems hide it in the file manager afterwards |
| Upload location | The document root for site-wide rules, or any subdirectory to scope the rules to that folder and below |
| Syntax highlighting | Around 24 recognised directive keywords, with strings and numbers coloured separately |
| Network | None after page load |
- Back up the existing .htaccess before you replace it. WordPress, Laravel and most CMS installers write their own rewrite block into that file, and pasting over it breaks routing instantly.
- Keep this file small on a busy site. Apache reads .htaccess on every request, and in every parent directory of the requested path — configuration in the main server config or a vhost is parsed once instead.
- Never enable HSTS until HTTPS works on every subdomain you own. The includeSubDomains directive is remembered by the browser for a year, and a subdomain still on plain HTTP becomes unreachable for that whole period.
- Test the HTTPS redirect from behind a proxy or CDN before you rely on it. A terminating proxy often leaves %{HTTPS} off on the origin, which turns the rule into a redirect loop.
- The hotlink rule allows an empty referer on purpose: without that, direct image loads, some privacy extensions and several browsers would be blocked from your own images.
- Setting a one-year cache on CSS and JavaScript is only safe if your build puts a hash in the file name. Without cache busting, a returning visitor keeps the old bundle for a year.
- The IfModule wrappers mean a missing module silently skips a block rather than crashing the server — which also means a header you cannot find may just be a module that is not loaded. Check with apachectl -M.
- If nothing at all takes effect, the usual cause is AllowOverride None in the server configuration; on your own server the file is simply ignored until that is changed.
- Nine ready directive blocks
- Per-block settings fields
- IfModule guards on module-dependent blocks
- Named custom rule blocks
- Syntax-highlighted live preview
- Copy to clipboard
- Download as .htaccess
- Force every visitor onto HTTPS after installing a certificate.
- Add the standard security headers a penetration test or a security scanner asked for.
- Give a single-page app the rewrite fallback it needs so deep links stop returning 404.
- Stop other sites from embedding your images and burning your bandwidth.
- Set sensible cache lifetimes on static assets to cut repeat-visit load time.
- Make sure .env and .git are not readable over the web on a shared host where you cannot change the server configuration.
Related tools
View allWorks well with this5
More in Data & Dev12
Updated