.htaccess Generator

Generate Apache htaccess

Basic Settings

Security

Performance

Error Pages

Redirects

.htaccess
# Generated by ToolNest .htaccess Generator
# 

# Force HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Remove WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]

# Default index file
DirectoryIndex index.php

# Disable directory listing
Options -Indexes

# Protect .htaccess
<Files .htaccess>
    Order allow,deny
    Deny from all
</Files>

# Block bad bots
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (bot|crawl|slurp|spider|mediapartners) [NC]
RewriteCond %{HTTP_USER_AGENT} !(googlebot|bingbot|yandex|duckduckbot) [NC]
RewriteRule .* - [F,L]

# Enable Gzip compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
    AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript
    AddOutputFilterByType DEFLATE application/json application/xml application/xhtml+xml
    AddOutputFilterByType DEFLATE image/svg+xml font/ttf font/otf
</IfModule>

# Browser caching
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/html "access plus 3600 seconds"
    ExpiresByType text/css "access plus 2592000 seconds"
    ExpiresByType text/javascript "access plus 2592000 seconds"
    ExpiresByType application/javascript "access plus 2592000 seconds"
    ExpiresByType image/jpeg "access plus 2592000 seconds"
    ExpiresByType image/png "access plus 2592000 seconds"
    ExpiresByType image/gif "access plus 2592000 seconds"
    ExpiresByType image/webp "access plus 2592000 seconds"
    ExpiresByType image/svg+xml "access plus 2592000 seconds"
    ExpiresByType font/woff2 "access plus 31536000 seconds"
</IfModule>

# Custom error pages
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html

Frequently Asked Questions

What is .htaccess?
.htaccess is an Apache server configuration file that controls URL redirects, security settings, caching, and access rules for directories and files on your website.
Do I need .htaccess for all servers?
No, .htaccess is Apache-specific. Nginx uses nginx.conf, IIS uses web.config. Many modern hosts use Nginx, so check your server type before using .htaccess rules.
What are common .htaccess uses?
URL redirects (www to non-www), HTTPS enforcement, custom error pages, hotlink protection, password protection, caching headers, and blocking bad bots.