🛠️ Developer & Web

.htaccess Generator

Tick the rules you need — get a ready-to-upload .htaccess.

  • 10rule switches
  • 1 hopHTTPS + www redirect
  • 301redirect list
  • Apacheand LiteSpeed

    

How to use the .htaccess Generator

  1. 1

    Enter your domain and www choice

    Redirect to non-www, to www, or leave the host alone. The domain is used in the file’s header comment.

  2. 2

    Tick the rules you want

    HTTPS, caching, gzip, directory listing, sensitive-file blocking, security headers, SPA fallback, clean URLs and font CORS.

  3. 3

    Add a 404 page and redirects

    Optional custom 404 path, plus one /old /new pair per line for permanent redirects.

  4. 4

    Download and upload

    Save the file as .htaccess in your site root, then test in a private window.

Features

Single canonical redirect

HTTP→HTTPS and www handling combined into one 301, avoiding chains.

Module-safe blocks

Rewrite, header, deflate and expires rules sit inside <IfModule> checks.

Clean URLs

/about.html redirects to /about, which is served from about.html or about.php.

SPA fallback

Unknown paths are served index.html so client-side routes survive a refresh.

Sensitive files

Blocks dotfiles, .env, .sql, .log, backups, composer files and .git.

Smart caching

HTML and JSON are never cached; CSS, JS, images and fonts are cached for a year.

Who uses it — and for what

Shared hosting sites

Hostinger, cPanel and similar Apache or LiteSpeed hosts.

Moving to HTTPS

Force secure URLs after installing an SSL certificate.

Site migrations

Keep old URLs working with 301 redirects.

Static React/Vue builds

Host an SPA on ordinary shared hosting.

What .htaccess does

.htaccess is a configuration file for Apache and LiteSpeed web servers — used by most shared hosts such as Hostinger, Bluehost, GoDaddy and Namecheap. Placed in your site’s root folder (usually public_html), it controls redirects, URL rewriting, caching, compression and access rules without touching the server config. This generator writes the most common rules in a safe order: HTTPS and www canonicalisation first (in one redirect, to avoid chains), then your 301 redirects, rewrites, error pages, caching and security.

Upload safely

  • Back up your existing .htaccess first — WordPress and other CMSs add their own rules; keep them below these.
  • Upload as plain text named exactly .htaccess (with the leading dot, no .txt).
  • If the site shows a 500 error, a module may be missing — remove the last block you added.
  • Test redirects in a private window; browsers cache 301s aggressively.

Use one canonical version of your domain (https + www or non-www) for SEO — it consolidates links and avoids duplicate content.

What each block does

OptionDirectives writtenNeeds module
Disable directory listingOptions -IndexesCore (host must allow Options overrides)
Custom 404ErrorDocument 404 /404.htmlCore
HTTPS / wwwRewriteCond + RewriteRule … [L,R=301,NE]mod_rewrite
Clean URLsA rule on %{THE_REQUEST} that 301s visible .html/.php URLs, then internal rewrites to the file that existsmod_rewrite
SPA fallbackRewriteBase /, skip index.html, and if not a file or folder rewrite to /index.htmlmod_rewrite
Redirect listRedirect 301 /old /newmod_alias
Block sensitive files<FilesMatch> with Require all denied, and RedirectMatch 404 /\.gitCore, Apache 2.4 syntax
Security headersnosniff, SAMEORIGIN, referrer policy, a Permissions-Policy turning off camera, microphone and geolocation, plus HSTS when HTTPS is onmod_headers
Font CORSAccess-Control-Allow-Origin "*" for ttf, otf, eot, woff, woff2mod_headers
GzipAddOutputFilterByType DEFLATE for text, JSON, XML, SVG and font typesmod_deflate
CachingExpiresByType: HTML/JSON/XML 0 s, CSS/JS/images/fonts 1 year, everything else 1 monthmod_expires

Pitfalls on real hosting

  • Redirect loops behind a proxy or CDN. If Cloudflare (or another proxy) talks to your server over plain HTTP, Apache always sees %{HTTPS} off and keeps redirecting. Use the CDN’s “Full” SSL mode, or check %{HTTP:X-Forwarded-Proto} instead, as your host documents.
  • WordPress and CMS rules. Keep the block WordPress writes (# BEGIN WordPress … # END WordPress) and do not tick the SPA or clean-URL options, which conflict with its own front-controller rewrite.
  • Redirect vs RewriteRule order. Redirect lines (mod_alias) and rewrite rules run in different modules, so a path matched by both can behave unexpectedly. Test each old URL with curl -I.
  • One-year caching. Only safe when CSS and JS file names change on every release (for example app.4c1e.js). Otherwise visitors may see old styles; the generated comment reminds you to version file names.
  • HSTS is sent for a year without includeSubDomains. Only enable it once HTTPS works everywhere on the site.
  • 500 Internal Server Error after upload usually means a directive is not allowed by the host’s AllowOverride setting. Remove blocks one by one to find it.

.htaccess versus server config

Apache re-reads .htaccess files on every request in each directory, which is slower than putting the same rules in the main server configuration — but on shared hosting .htaccess is often the only option. On a VPS with full access, consider moving the rules into the virtual host. Sites on nginx ignore .htaccess entirely; the nginx config generator produces the equivalent server block. To learn what each redirect code means, see HTTP status codes.

Worked examples

HTTPS + non-www in one redirect

With the defaults the rewrite block is:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

Read it as: if the request is not HTTPS or the host starts with www, capture the host without www as %1 and send one permanent redirect to https:// plus that host and the original path. http://www.example.com/shop?x=1 goes straight to https://example.com/shop?x=1.

A redirect list

Lines like /old-page /new-page and /blog/2019-offer /offers become Redirect 301 /old-page /new-page and Redirect 301 /blog/2019-offer /offers. Lines that do not have exactly two parts, or whose first part does not start with /, are skipped.

Pro tips

  • Download your current .htaccess before replacing it — one wrong line can take the whole site offline.
  • Test redirects in a private window; browsers remember 301s and will hide your fixes.
  • After enabling gzip, check a page’s response for Content-Encoding: gzip in DevTools.

Glossary

mod_rewrite
The Apache module that rewrites and redirects URLs using rules and conditions.
RewriteCond
A condition that must be true for the next RewriteRule to run.
NE flag
No-escape: stops Apache re-encoding characters like % and # in the target URL.
AllowOverride
The server setting that controls which directives .htaccess files may use.

Frequently asked questions

Where do I put the .htaccess file?

In your website’s root folder, usually public_html.

Does .htaccess work on Nginx?

No — Nginx ignores it; use the Nginx Config Generator instead.

301 or 302 redirect?

301 for permanent moves (passes SEO value); 302 for temporary ones.

Does the domain I type appear in the rules?

Only in the header comment. The redirect captures the host from each request, so the same file works if you rename the domain.

Will this work on LiteSpeed hosting?

Generally yes — LiteSpeed reads Apache-style .htaccess rules, including mod_rewrite.

Can I use it for a WordPress site?

Yes for HTTPS, www, caching, gzip and headers. Place the generated rules above the WordPress block and leave SPA and clean URLs off.

Found it useful? Share the .htaccess Generator:

Last updated 2026-11-28 · Built by Vinayak Agarwal · Part of Developer & Web (77 tools)