.htaccess Generator

Build an Apache .htaccess file with 301/302 redirects, HTTPS and www redirects, browser caching, Gzip and security headers. The file updates as you pick options.

Connect:

Configuration Options

Needed for www redirects and hotlink protection

Redirects

Old path starts with /. The new URL can be a path or a full https:// address. A redirect also covers everything under the old path, so /blog sends /blog/post to the same place under the new URL.

Add any additional Apache directives you need

Generated .htaccess

# .htaccess generated at codewithmuh.com
# Put this file in your site's document root. Apache reads it on every request, so no restart is needed.

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Force HTTPS
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</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"
    Header always set Strict-Transport-Security "max-age=31536000" env=HTTPS
</IfModule>

# Browser caching
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 month"
    ExpiresByType text/html "access plus 0 seconds"
    ExpiresByType application/json "access plus 0 seconds"
    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/avif "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
</IfModule>

# Gzip compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript
    AddOutputFilterByType DEFLATE application/javascript application/json application/xml application/rss+xml
    AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>

# Don't list folder contents when there is no index file
Options -Indexes

Save it as .htaccess in your site's root folder. The download is named htaccess.txt because browsers hide files that start with a dot, so rename it after uploading.

⚡ Quick Usage

# 1. Save the text as .htaccess in your document root
# 2. Apache reads it on every request: no reload needed
# 3. Load your site. A 500 error means a module is missing
#    or AllowOverride is off; check the error log:
tail -f /var/log/apache2/error.log

🔒 Security

Security headers, HTTPS redirects, hotlink protection and directory listing control.

⚡ Performance

Adds browser caching and gzip compression to improve website loading speed and reduce bandwidth usage.

🔄 Redirects

Add as many 301/302 redirects as you need for moved pages, changed URLs or site migrations.