Skip to content

.htaccess Generator

Embed

Paste this where you want .htaccess Generator on your site. Adjust the height to fit.

<iframe src="https://www.browserutils.dev/embed/htaccess-generator/" width="100%" height="500" frameborder="0" loading="lazy" title=".htaccess Generator"></iframe>
<p style="font-size:12px;margin:4px 0 0"><a href="https://www.browserutils.dev/tools/htaccess-generator/">.htaccess Generator</a> by BrowserUtils</p>

Generate .htaccess rules (redirects, rewrites, etc.).Runs in this tab.

->

How to use .htaccess Generator

  1. Set the checkboxes you want: Force HTTPS, Force www, Remove www, Enable GZIP, Browser Caching, Disable Directory Listing, Custom Error Pages, and Block Hotlinking.
  2. If Browser Caching is on, set Cache Duration (days). The default is 30. Custom Error Pages and Block Hotlinking reveal their own fields.
  3. 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

Questions

How do I use this htaccess RewriteRule generator?
Each filled redirect row becomes a RewriteRule. /blog/old-post to /blog/new-post as 301 is RewriteRule ^blog/old-post$ /blog/new-post [R=301,L]. The leading slash on the from path is removed. The page opens with /old-page to /new-page, Force HTTPS, GZIP, and 30-day caching on.
What is the difference between a 301 and a 302 in the redirect row?
The row's select writes the status into the flag: [R=301,L] or [R=302,L]. 301 is the permanent redirect. 302 is the temporary one. Force HTTPS always uses R=301, separate from the rows.
Does this generate nginx rules?
The output is Apache: RewriteEngine, mod_deflate, and mod_expires. Force www and Remove www are mutually exclusive in the output. If both are ticked, neither www block is written. Caching covers jpeg, png, gif, svg, css, js, and woff2.

More in Web & Networking

All 23 web & networking