.htaccess Generator
Generate .htaccess rules (redirects, rewrites, etc.).Runs in this tab.⌘ ↵ runs it
How to use .htaccess Generator
- Set the checkboxes you want: Force HTTPS, Force www, Remove www, Enable GZIP, Browser Caching, Disable Directory Listing, Custom Error Pages, and Block Hotlinking.
- If Browser Caching is on, set Cache Duration (days). The default is 30. Custom Error Pages and Block Hotlinking reveal their own fields.
- Edit the redirect rows (from, to, and 301 or 302). The page starts with /old-page to /new-page as a 301. Empty from or to rows are skipped. Copy the generated file.
The .htaccess file the form writes
The form is Apache only: mod_rewrite, mod_deflate, and mod_expires. It does not emit nginx.
Defaults
Force HTTPS on, Force www off, Remove www off, Enable GZIP on, Browser Caching on at 30 days, and one 301 from /old-page to /new-page:
RewriteEngine On
# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect /old-page -> /new-page
RewriteRule ^old-page$ /new-page [R=301,L]
# Enable GZIP Compression
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css
AddOutputFilterByType DEFLATE text/javascript application/javascript application/json
AddOutputFilterByType DEFLATE application/xml text/xml
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE font/ttf font/otf font/woff font/woff2
</IfModule>
# Browser Caching
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 30 days"
ExpiresByType image/png "access plus 30 days"
ExpiresByType image/gif "access plus 30 days"
ExpiresByType image/svg+xml "access plus 30 days"
ExpiresByType text/css "access plus 30 days"
ExpiresByType application/javascript "access plus 30 days"
ExpiresByType font/woff2 "access plus 30 days"
</IfModule>
HTTPS is forced with %{HTTPS} off, then a 301 to https://%{HTTP_HOST}. Caching lists jpeg, png, gif, svg, css, js, and woff2. The redirect row drops the leading slash on the from path. An empty from or to cell is skipped. The sample /old-page row is real output until you edit it.
Force www
Tick Force www and leave Remove www off. The www block is:
# Force www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Force www and Remove www are mutually exclusive in the output. If both are ticked, neither block is written. The www redirect hard-codes https://.
A custom 301
From /blog/old-post to /blog/new-post, type 301:
# Redirect /blog/old-post -> /blog/new-post
RewriteRule ^blog/old-post$ /blog/new-post [R=301,L]
Choose 302 on the row and the flag becomes [R=302,L].
Optional blocks, off by default: Custom Error Pages (ErrorDocument for the 404 and 500 paths), Block Hotlinking (uses the domain field), and Disable Directory Listing (Options -Indexes). Read the HTTPS and www rules against your real host before you drop the file on a server. A host that is already behind HTTPS, or a name that should not gain www, needs those boxes changed first.
Next step
Turn a click-through into an interactive demo
AtriumKit records your web app once in Chrome and turns every click into a step of a demo you can embed or share. There’s a free plan.
Try AtriumKit freeQuestions
How do I use this htaccess RewriteRule generator?
What is the difference between a 301 and a 302 in the redirect row?
Does this generate nginx rules?
Comments
More in Web & Networking
- URL Parser
- HTTP Status Codes Reference
- HTTP Headers Reference
- MIME Types Reference
- HTTP Methods Reference
- IP Address Converter
- CIDR Calculator
- DNS Record Types Reference
Comments