induwara.lk
induwara.lkDeveloper · Apache

.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.

By Induwara AshinsanaUpdated Jul 17, 2026
.htaccess Generator
Presets

Hostname only, no https:// or path. Leave blank for domain-agnostic rules.

Redirect every HTTP request to HTTPS (301).

Canonical host

Stop Apache serving a file list when there is no index file.

Compress text responses with mod_deflate.

Set far-future cache lifetimes for static assets (mod_expires).

Security headers
Custom 301 / 302 redirects

None yet — add a row to redirect an old path to a new URL.

Custom error pages

None yet — add a row to serve your own page for a status code.

Block other sites from embedding your images.

Require an HTTP Basic Auth login (mod_authn_file).

.htaccess
# .htaccess — generated by induwara.lk/tools/htaccess-generator
# 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

# GZIP compression
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json application/xml application/rss+xml image/svg+xml application/x-font-ttf application/vnd.ms-fontobject font/opentype
</IfModule>

# Browser caching
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType text/html "access plus 0 seconds"
</IfModule>

# 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>
40 lines1,489 bytes6 sections

Save this as a plain file named .htaccess(no extension) in your site's document root. Every directive follows the Apache 2.4 docs — sources cited below the tool.

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.

  1. Force HTTPS emits a mod_rewrite rule: when %{HTTPS} is off, redirect ^(.*)$ to https://%{HTTP_HOST}%{REQUEST_URI} with the [L,R=301] flags.
  2. 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 reaches https:// + the right host.
  3. Custom redirects use mod_alias — one Redirect 301 /old https://…/new line per row, with the status code taken from the row (301 permanent, 302 temporary).
  4. Error pages, directory listing and Basic Auth use Apache core and mod_authn_file: ErrorDocument, Options -Indexes, and the AuthType Basic … Require valid-user block.
  5. GZIP (mod_deflate), caching (mod_expires), hotlink protection (mod_rewrite with 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

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

Rate this tool
Be the first to rate

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.