Skip to main content
SEO & Webmaster Essential

Apache .htaccess & Nginx Redirect / Security Rules Generator

Generate high-performance Apache .htaccess and Nginx configuration snippets for 301/302 redirects, HTTPS canonicalization, security headers (CSP, HSTS, X-Frame-Options), caching, and Gzip/Brotli compression.

Canonical URL redirects: Force HTTPS (SSL), enforce or strip www. subdomains, and eliminate trailing slashes
Automated 301 permanent and 302 temporary rewrite rule builder with RegEx pattern matching
Enterprise Security Headers generator: Content Security Policy (CSP), Strict-Transport-Security (HSTS), X-Content-Type-Options, and X-Frame-Options
Browser caching (mod_expires / Cache-Control) and Gzip/Brotli compression directives for Google PageSpeed 100/100
Dual output generator: exports production-ready Apache .htaccess directives and equivalent Nginx server {} blocks
01 // WEB SERVER CONFIGURATIONApache .htaccess & Nginx Configuration Studio.htaccess • SPA Preset
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# ==============================================================================
# Apache .htaccess Configuration generated by WebCraftKit (webcraftkit.com)
# Precision Server Architecture • 100% Client-Side Generated
# Generated at: 2026-11-15
# ==============================================================================
 
# Enable Rewrite Engine
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
 
# --- 1. Force HTTPS / SSL Redirect ---
# Cloudflare / Load Balancer reverse proxy check
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
 
# --- 2. Domain Canonicalization ---
# Force Non-WWW (strip www.)
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L,NE]
 
# --- 3. Clean URLs & Routing ---
# Remove .html extension from URL
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
 
# --- 5. Custom 301 & 302 Redirects ---
Redirect 301 /old-about /about
RewriteRule ^legacy-blog/(.*) /blog/$1 [R=301,L,NE]
 
</IfModule>
 
# --- 4. Security & Server Hardening ---
# Disable Directory Indexing / File Browsing
Options -Indexes
 
# Mask Server Signature
ServerSignature Off
 
# Protect Sensitive Files & Directories (.git, .env, .htaccess, package.json, logs)
<FilesMatch "(^\.|\.(env|git|htaccess|htpasswd|ini|log|conf|bak|sql|sh|yml|yaml|json)$)">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</FilesMatch>
 
# Block Malicious User-Agents and Vulnerability Scanners
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_USER_AGENT} (libwww-perl|wget|python|nikto|curl|scan|clshttp|archiver|loader|email|harvest|extract|grab|miner) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} (<|>|'|%0A|%0D|%27|%3C|%3E|%00) [NC]
RewriteRule .* - [F,L]
</IfModule>
 
# Security Headers
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-Content-Type-Options "nosniff"
Header always set X-XSS-Protection "1; mode=block"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=()"
</IfModule>
 
# --- 6. Custom Error Documents ---
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html
 
# --- 7. Gzip / Deflate Compression ---
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/woff
AddOutputFilterByType DEFLATE font/woff2
</IfModule>
 
# --- Leverage Browser Caching (mod_expires & Cache-Control) ---
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 month"
# Images & Media
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "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"
# CSS, JavaScript
ExpiresByType text/css "access plus 1 year"
ExpiresByType text/javascript "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
# Fonts
ExpiresByType font/woff2 "access plus 1 year"
ExpiresByType font/woff "access plus 1 year"
# HTML Document
ExpiresByType text/html "access plus 0 seconds"
</IfModule>
WebCraftKit platform · 100% client-side engine

Air-Gapped Privacy & Zero-Latency Developer Utilities

Every cryptographic algorithm, schema transformer, color space converter, and binary extractor runs entirely in your browser RAM. Your tokens, API secrets, and source code are never sent to external servers.

0 B server telemetry
·
< 2ms execution
·
80 production tools
Read architecture story →
Comprehensive technical manual

Server Configuration Mastery: Optimizing Apache .htaccess and Nginx for SEO, Speed, and Security

In-depth specifications, architectural mechanics, real-world code implementations, and industry best practices.

01

The Architecture of Web Server Configuration: Apache mod_rewrite vs Nginx

Web servers serve as the frontline gatekeepers for all HTTP/HTTPS requests. Apache HTTP Server provides per-directory decentralized configuration files named .htaccess, which are evaluated dynamically at runtime by modules such as mod_rewrite, mod_headers, and mod_expires. In contrast, Nginx evaluates centralized configuration blocks inside nginx.conf. Understanding how rewrite rules, status codes (301 Permanent vs 302 Temporary), and condition flags operate is fundamental to maintaining high search engine rankings and preventing duplicate content penalties.

Implementation example
# Apache mod_rewrite Canonical Rule Architecture
RewriteEngine On
RewriteBase /

# Force HTTPS & Strip WWW in a Single Redirect
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
02

SEO Canonicalization: HTTPS, WWW Normalization, and Clean URLs

Search engine crawlers evaluate http://example.com, https://example.com, http://www.example.com, and https://www.example.com as four distinct websites. Failing to canonicalize these variants leads to split PageRank, diluted link equity, and duplicate content warnings in Google Search Console. Implementing a single 301 permanent redirect consolidates all traffic to the canonical secure origin while preserving full link equity.

Implementation example
// Equivalent Nginx Canonical Server Configuration
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name www.example.com;
    return 301 https://example.com$request_uri;
}
03

Step-by-Step Server Rule Generation Workflow

  • • Configure production-grade server rules effortlessly:
  • • Step 1: Configure canonical domain settings (Force HTTPS SSL, WWW vs non-WWW preferences).
  • • Step 2: Add specific 301 URL redirects (e.g. Redirect 301 /old-page /new-page).
  • • Step 3: Toggle Enterprise Security Headers: HTTP Strict Transport Security (HSTS), X-Frame-Options (DENY), Content-Security-Policy (CSP), and X-Content-Type-Options (nosniff).
  • • Step 4: Enable mod_expires browser caching directives (1 year for images/fonts, 1 month for CSS/JS).
  • • Step 5: Copy the generated Apache .htaccess snippet or switch tabs to copy the Nginx server block.
Implementation example
# Enterprise Security Headers in Apache .htaccess
<IfModule mod_headers.c>
  Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
  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 Permissions-Policy "geolocation=(), microphone=(), camera=()"
</IfModule>
04

Browser Caching & Compression for Core Web Vitals (PageSpeed 100/100)

Serving assets with long-lived Cache-Control and Gzip/Brotli compression dramatically reduces Largest Contentful Paint (LCP) and bandwidth consumption.

Implementation example
# Apache Browser Caching (mod_expires) & Gzip (mod_deflate)
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
</IfModule>

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/css application/json application/javascript
</IfModule>
05

Security Hardening, Hotlink Protection & Troubleshooting 500 Errors

  • • Protecting your server infrastructure against unauthorized access:
  • • Block Sensitive Files: Explicitly forbid access to .env, .git, and configuration manifests.
  • • Hotlink Protection: Prevent external domains from leeching image bandwidth via HTTP Referer inspection.
  • • Diagnosing 500 Internal Server Errors: Apache 500 errors are almost always caused by syntax typos or referencing inactive modules. Always wrap directives inside <IfModule mod_name.c> guard blocks.
Implementation example
# Block Access to Hidden Files & Sensitive Assets
<FilesMatch "^\.(env|git|htaccess|htpasswd|ini|log|sh)$">
  Require all denied
</FilesMatch>
Knowledge base & clarifications

Frequently Asked Questions: .htaccess Generator

Got questions about how .htaccess Generator operates, client-side cryptographic safety, or performance limits? Explore common answers below.

What is the difference between a 301 and 302 redirect in .htaccess?

A 301 redirect is permanent, instructing search engine crawlers and browsers to transfer ~99% of SEO link equity to the destination URL. A 302 redirect is temporary and tells crawlers to retain the original URL in search indices.

How do I force HTTPS and remove www using .htaccess?

Enable RewriteEngine On, add RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} ^www\. [NC], followed by RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301].

Why does my .htaccess file cause a 500 Internal Server Error?

A 500 error typically indicates a syntax typo or the use of directives requiring Apache modules (like mod_rewrite, mod_headers, or mod_expires) that are not enabled on the server. Always wrap rules in <IfModule> conditional tags.

Can I use .htaccess files on Nginx or LiteSpeed servers?

LiteSpeed natively parses .htaccess files without changes. Nginx does not support .htaccess files directly; you must place the equivalent server {} directives inside your nginx.conf file (which our tool generates).

How does adding Cache-Control and Expires headers improve Core Web Vitals?

Browser caching directives instruct repeat visitors' browsers to load static assets (images, CSS, fonts) directly from local disk cache, lowering Largest Contentful Paint (LCP) and server round-trips.

Complementary utilities
View all in SEO & Webmaster →