1. Home
  2. Developer
  3. .htaccess Generator

.htaccess Generator

Pick options and get Apache .htaccess rules, with warnings for risky combinations.

For every format and option in one place, use Server Config Generators →

Runs in your browser · No upload

HTTPS and canonical URL


URL redirects


Error pages


Speed


Security headers


Access control

The Options directive needs AllowOverride Options in the server config; otherwise the site returns 500.
Behind a proxy such as Cloudflare, leaving this option off can cause a redirect loop. Turn on the proxy option above in that case.
Mixing Redirect (mod_alias) and RewriteRule (mod_rewrite) can make the order unclear for the same URL. Test the result.
.htaccess
# .htaccess (Apache 2.4) # Redirects <IfModule mod_alias.c> Redirect 301 /old-page /new-page </IfModule> # Rewrite rules <IfModule mod_rewrite.c> RewriteEngine On # force HTTPS RewriteCond %{HTTPS} !=on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] </IfModule> # Custom error pages ErrorDocument 404 /404.html # Disable directory listing Options -Indexes # Block hidden files such as .htaccess, .env, .git <FilesMatch "^\."> Require all denied </FilesMatch> # Compression <IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript application/javascript application/json application/xml application/xhtml+xml application/rss+xml application/atom+xml image/svg+xml font/ttf font/otf application/vnd.ms-fontobject </IfModule> <IfModule mod_brotli.c> AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/xml text/javascript application/javascript application/json application/xml application/xhtml+xml application/rss+xml application/atom+xml image/svg+xml font/ttf font/otf application/vnd.ms-fontobject </IfModule> # Browser caching <IfModule mod_expires.c> ExpiresActive On 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 text/css "access plus 1 month" ExpiresByType text/javascript "access plus 1 month" ExpiresByType application/javascript "access plus 1 month" ExpiresByType font/woff "access plus 1 year" ExpiresByType font/woff2 "access plus 1 year" ExpiresByType font/ttf "access plus 1 year" ExpiresByType font/otf "access plus 1 year" ExpiresByType text/html "access plus 0 seconds" </IfModule> # Security headers <IfModule mod_headers.c> Header always set X-Content-Type-Options "nosniff" Header always set X-Frame-Options "SAMEORIGIN" Header always set Referrer-Policy "strict-origin-when-cross-origin" Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()" </IfModule>

This file is for Apache; Nginx does not read .htaccess. If the server blocks AllowOverride or the module (mod_rewrite, mod_headers, mod_expires, mod_deflate) is off, you may get a 500 error, so back up the existing file before uploading.

How to use

  1. Pick the options you need, top to bottom. The defaults are HTTPS redirect, hidden directory listings, blocked dotfiles, text compression and caching.
  2. To send an old URL to a new one, add the path under URL redirects and choose the type (301 permanent, 302 temporary, 307 and 308 keep the request method).
  3. Read the warnings, then copy the .htaccess result or download it as a file.
  4. Upload it to the site’s top folder (often public_html) as .htaccess. If one exists, back it up first.

What each option writes

Option Apache module Result
Force HTTPS mod_rewrite 301 redirect after RewriteCond %{HTTPS} !=on
www canonical mod_rewrite 301 to either the www or the non-www address
URL redirects mod_alias Redirect 301 /old /new
Compression mod_deflate, mod_brotli smaller HTML, CSS, JS, JSON and SVG transfers
Browser caching mod_expires long for images and fonts, 0 seconds for HTML
Security headers mod_headers nosniff, X-Frame-Options, Referrer-Policy, HSTS and more
Hotlink protection mod_rewrite 403 when another site loads your images directly
Hide listings, block dotfiles core, access control Options -Indexes, <FilesMatch "^\.">

Every module rule is wrapped in <IfModule> so a missing module does not cause a 500 error. Directives that cannot be wrapped, such as Options -Indexes, fail if the server does not allow AllowOverride Options; the tool warns about it.

Example output

With only HTTPS redirect, www canonical, one redirect, a 404 page, hidden listings and blocked dotfiles selected (compression, caching and headers off) you get:

# .htaccess (Apache 2.4)

# Redirects
<IfModule mod_alias.c>
  Redirect 301 /old-page /new-page
</IfModule>

# Rewrite rules
<IfModule mod_rewrite.c>
  RewriteEngine On
  # non-www to www
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteCond %{HTTP_HOST} !^(localhost|[0-9.]+)(:[0-9]+)?$
  RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  # force HTTPS
  RewriteCond %{HTTPS} !=on
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

# Custom error pages
ErrorDocument 404 /404.html

# Disable directory listing
Options -Indexes

# Block hidden files such as .htaccess, .env, .git
<FilesMatch "^\.">
  Require all denied
</FilesMatch>

When you pick both www canonical and HTTPS, the rules are ordered so http://example.com reaches https://www.example.com in a single hop. Two chained redirects cost both crawlers and visitors.

Before you use it

  • Behind a proxy or CDN (Cloudflare and similar) the server may always see plain HTTP, and an HTTPS redirect then loops forever. Turn on the proxy option so the rule checks the X-Forwarded-Proto header instead.
  • HSTS makes browsers remember HTTPS-only for max-age. Use includeSubDomains only when every subdomain supports HTTPS. Preload list eligibility needs a max-age of at least one year and includeSubDomains.
  • Authentication plus an IP rule passes when either one matches (OR). Requiring both needs a hand-written <RequireAll>.
  • The www rule would also send a subdomain such as blog.example.com to www.blog.example.com. Do not use it if subdomains share the folder.
  • Nginx ignores .htaccess; use the Nginx Config Generator there.

Frequently asked questions

I uploaded the file and got a 500 error.

Usually a required module is off, AllowOverride blocks the directive, or one line is wrong. The server error log names the offending line. Restore the backup you made before uploading.

What is the difference between 301 and 302?

301 is a permanent move, so search engines transfer ranking to the new address; 302 is temporary and keeps the original. See HTTP Status Codes for what each code means.

Where does the file go?

In the site’s web root (document root). A .htaccess in a subfolder applies only inside that folder. Its name starts with a dot, so enable hidden files in your FTP client to see it.

Does it control search engine crawling?

No. Crawl rules belong in robots.txt; use the robots.txt Generator.

For network ranges see the Firewall Rule Generator, and for the .htpasswd file behind password protection use the htpasswd Generator. What you enter is processed in your browser and never sent to a server.

Can't find the tool you need?

Tell us what you want to do. We build the most requested tools first.