.htaccess Generator — HTTPS, 301 Redirects & Security
Build a valid Apache .htaccess file from simple toggles: force HTTPS, canonicalise www vs non-www, add 301/302 redirects, password-protect folders, and set caching and security headers. Every module block is <IfModule>-guarded, generated in your browser, nothing uploaded.
How it works
The generator is a deterministic string builder. Each option you enable appends a documented directive block in the order Apache evaluates configuration: rewrite rules run before content is served, while headers, compression and caching are applied independently. There is no server round-trip — the file is assembled on this page.
- Force HTTPS emits a
mod_rewriterule: when%{HTTPS}is off, redirect^(.*)$tohttps://%{HTTP_HOST}%{REQUEST_URI}with the[L,R=301]flags. - Canonical host adds a second rewrite: non-www matches
^www\.(.+)$and redirects to the captured bare host; www matches hosts that do not start with www and prepends it. Ordered after the HTTPS rule so a single canonical hop reacheshttps://+ the right host. - Custom redirects use
mod_alias— oneRedirect 301 /old https://…/newline per row, with the status code taken from the row (301 permanent, 302 temporary). - Error pages, directory listing and Basic Auth use Apache
coreandmod_authn_file:ErrorDocument,Options -Indexes, and theAuthType Basic … Require valid-userblock. - GZIP (
mod_deflate), caching (mod_expires), hotlink protection (mod_rewritewith an[F]forbidden flag) and security headers (mod_headers) each append their own block.
Every block that depends on an optional module is wrapped in <IfModule mod_x.c> … </IfModule>, so a host missing that module skips the block instead of returning HTTP 500. The one deliberate exception is the password-protection block: leaving it unwrapped means a missing auth module makes the request fail rather than silently exposing a folder that was supposed to be locked. The tool cross-checks that every <IfModule>guard has a matching close tag before showing the “structure verified” badge. All syntax follows the Apache HTTP Server 2.4 documentation, last verified 2026-07-17.
Worked examples
HTTPS + non-www migration
Domain example.lk, Force HTTPS on, canonical = non-www, directory listing off, first three security headers.
# .htaccess — generated by induwara.lk/tools/htaccess-generator
# Site: example.lk
# Place in your document root, above any CMS-managed block (e.g. # BEGIN WordPress).
# Force HTTPS + canonical host
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
</IfModule>
# Disable directory listing
Options -Indexes
# Security headers
<IfModule mod_headers.c>
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>
A request to http://www.example.lk/contact hits the HTTPS rule → https://www.example.lk/contact, then the www rule → https://example.lk/contact. Two 301 hops, canonical destination reached.
Renamed posts + a custom 404
Two 301 redirects (/2021/old-title → /hello-world, /archive → /posts) and a custom 404 page.
# .htaccess — generated by induwara.lk/tools/htaccess-generator
# Site: blog.lk
# Place in your document root, above any CMS-managed block (e.g. # BEGIN WordPress).
# Custom redirects
<IfModule mod_alias.c>
Redirect 301 /2021/old-title https://blog.lk/hello-world
Redirect 301 /archive https://blog.lk/posts
</IfModule>
# Custom error pages
ErrorDocument 404 /404.html
/2021/old-title issues a permanent redirect; any unknown path renders /404.html with a real 404 status. mod_alias handles the exact-path redirects; ErrorDocument is a core directive, so no IfModule guard is needed.
Password-protect a staging folder
Directory protection on, .htpasswd path /home/exampleusr/.htpasswd, realm 'Staging'.
# .htaccess — generated by induwara.lk/tools/htaccess-generator
# Place in your document root, above any CMS-managed block (e.g. # BEGIN WordPress).
# Password-protect this directory (HTTP Basic Auth)
# Intentionally NOT wrapped in <IfModule>: if the auth module is missing the
# request must fail, never fall open.
AuthType Basic
AuthName "Staging"
AuthUserFile /home/exampleusr/.htpasswd
Require valid-user
Any request under the directory prompts for HTTP Basic Auth, validated against the .htpasswd file made with the htpasswd Generator. The block is intentionally not wrapped in IfModule so a missing auth module fails closed.
Frequently asked questions
Sources & references
- Apache HTTP Server — .htaccess files how-to
- Apache — mod_rewrite (RewriteCond / RewriteRule flags)
- Apache — mod_alias (Redirect 301 / 302)
- Apache — mod_headers (security headers / HSTS)
- Apache — Authentication and Authorization how-to
- Apache core — ErrorDocument & Options directives
The directive syntax on this page was last cross-checked against the Apache HTTP Server 2.4 documentation on 2026-07-17. Back up your existing .htaccess before replacing it, and reload the site once to confirm nothing 500s.
Related tools
Comments & feedback
Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.
Found a bug, edge case, or want to suggest an improvement?
Email me at [email protected] — most fixes ship within 24 hours.