Skip to content
SentrueTools
Tools

.htaccess Generator

liveFree

Build a clean, commented .htaccess for HTTPS, www, trailing slashes, redirects, and caching.

How to use .htaccess Generator

What is .htaccess?

.htaccess is a configuration file that the Apache web server reads from your site’s folders on every request. It lets you change how the server answers without access to the main server configuration, which is why shared hosts and most WordPress installs rely on it. The most common jobs are SEO jobs: forcing HTTPS, choosing between www.example.com and example.com, normalizing trailing slashes and redirecting old URLs after a migration.

It is also unforgiving. One typo returns a 500 error for the whole site, and rules that each look right can combine into redirect loops or chains. The .htaccess Generator writes the file for you from a short form, explains every section in comments, and checks the result as you type.

How to use the generator

  1. Enter your domain (optional but recommended). With it, host rules match your exact domain and redirects go straight to your canonical address.
  2. Choose canonicalization. Turn on Force HTTPS, choose Remove www or Add www, and pick a trailing-slash policy.
  3. Add redirects, one per line, or open them from Redirect Mapper (see below).
  4. Pick performance and security presets. Compression, browser caching, security headers, blocking hidden files and directory listings are on by default; each has a short explanation.
  5. Read the problems panel. Errors, warnings and notes update as you type. Fix the errors before you upload anything.
  6. Download or copy the file and upload it to the document root of your site, usually public_html or www. Keep a copy of the old file.

Your settings are saved in this browser, so you can come back and adjust them. Reset restores the defaults, with an Undo.

Writing redirect lines

Each line is a source and a target, with an optional status code:

/old-page /new-page
/summer-sale /offers 302
/discontinued-product 410
/blog/* /articles/*
/search?q=shoes /shop/shoes
https://example.com/full-url-works-too /target
  • The default status is 301 (moved permanently), which is what you want for a migration. 302 and 307 are temporary, 308 is a permanent redirect that keeps the request method, and 410 tells crawlers the page is gone on purpose. A 410 needs no target.
  • A trailing * covers everything below a folder. If the target also ends in *, the rest of the path is carried over, so /blog/my-post goes to /articles/my-post. Without a * in the target, every URL in the folder goes to the same page.
  • A source with a query string matches only that exact query, and the old query is dropped from the redirect.
  • Spaces, tabs, commas and arrows (-> or →) all separate the columns, so you can paste two columns straight from a spreadsheet. Lines starting with # are comments, and a source,target header row is skipped.
  • Sources may be paths or full URLs; only the path and query matter. Targets may be paths on your site or full URLs on another site.

Why the rule order matters

Apache has two redirect engines, mod_alias (Redirect, RedirectMatch) and mod_rewrite (RewriteRule). Inside .htaccess, every mod_rewrite rule runs before any mod_alias rule, whatever order they appear in. Files that mix the two often behave differently from how they read.

The generator writes every redirect as a mod_rewrite rule, so the file runs top to bottom and the first matching rule wins:

  1. Blocking rules, such as hidden folders and unwanted bots.
  2. Your redirects, most specific first: sources with a query string, then exact paths, then folder wildcards from the longest prefix to the shortest. If you list /blog/* above /blog/special-offer, the specific line is moved up so it still wins, and a note tells you so.
  3. Canonical host (add or remove www).
  4. HTTPS.
  5. Trailing slash.

Redirects come before the canonical rules and, when your domain is set, point at the full canonical address. A visitor following an old link to http://www.example.com/old-page reaches https://example.com/new-page in a single 301 instead of two or three hops. Every hop costs time, and search engines follow only a limited number of them.

Canonicalization options

Force HTTPS redirects every http:// request to https://. The rule also checks the X-Forwarded-Proto header, so it does not loop when your site sits behind Cloudflare or a load balancer that handles HTTPS and talks to Apache over plain HTTP.

Remove www and Add www pick one host name for the whole site. With your domain entered, the rule matches only that domain. Without it, the generator writes a generic rule: Remove www works for any host, but Add www would also redirect subdomains, IP addresses and localhost, so the problems panel warns you until you enter the domain.

Trailing slash makes /page/ and /page one URL. Remove keeps real directories intact; Add leaves files such as /style.css alone. Choose the form your CMS already links to, so that internal links never redirect.

What the lint checks

  • Redirect loops (errors): a page redirected to itself, such as /pricing to /pricing/; a folder wildcard whose target sits inside the same folder, such as /blog/* to /blog/new/*; and cycles such as /a to /b and /b back to /a.
  • Chains (warnings): /a to /b when /b also redirects. The warning names the final target so you can point /a straight at it. A redirect to a URL that another line marks as 410 is flagged too.
  • Conflicts (warnings): the same source listed twice with different targets. Only the first line would ever apply.
  • Targets that redirect again (warnings): a target on your own domain written as http:// while HTTPS is forced, or with www while www is removed.
  • Missing RewriteEngine On (errors): custom rules that use RewriteRule or RewriteCond when nothing above them turns the engine on, so Apache would ignore them, and custom rules that turn it off.
  • Unreadable lines and invalid error pages (errors), with the line number.

Each item shows an icon and a label (Error, Warning or Note), not just a color.

Performance and security presets

  • Compression uses Brotli when the server has mod_brotli and falls back to gzip (mod_deflate) otherwise, for HTML, CSS, JavaScript, JSON, XML and SVG.
  • Browser caching sets Expires headers with mod_expires. Standard caches CSS and JavaScript for a week and images and fonts for a month. Long-lived caches assets for a year and is only safe when file names change on every deploy, such as app.3f9a2c.js.
  • Security headers add X-Content-Type-Options, X-Frame-Options, Referrer-Policy and a restrictive Permissions-Policy. HSTS tells browsers to use HTTPS for a year; turn it on only once every page works over HTTPS.
  • Blocking returns 403 for hidden files and folders such as .git and .env (but not .well-known, which certificate renewals need), common backup and log files, and optionally a list of bots. Disabling directory listings stops Apache from showing the contents of folders without an index page.

The compression, caching and header blocks are wrapped in <IfModule>, so a server without one of those modules skips that block instead of failing.

Importing redirects from Redirect Mapper

After matching old and new URLs in Redirect Mapper, choose Open in .htaccess Generator in its export bar. The accepted redirects arrive as lines in the Redirects box, with folder rules kept as * wildcards; your other settings stay as they were, and Undo restores your previous redirects. The free limit is the same as for a download: up to 200 old URLs, or the first 200 for larger projects without a pass.

Testing before you go live

Upload the file to a staging copy first. Then request a handful of URLs with a tool that shows status codes (curl -I, or your browser’s network panel): an http://www URL, an old URL from your redirect list, a URL with a trailing slash and one with a query string. Each should answer with a single 301 to a page that returns 200. If the server answers 500, the host may not allow some directives in .htaccess; remove the newest section and try again.

Privacy

The generator runs entirely in your browser. Your settings and redirect lists are saved only in this browser’s storage and are never uploaded.

Frequently asked questions

Where do I put the .htaccess file?

Upload it to the document root of your site (usually public_html or www). Apache reads it on every request.

Does .htaccess work on Nginx?

No. .htaccess is Apache-only (LiteSpeed reads it too). Nginx needs rules in its server configuration; Redirect Mapper can export Nginx redirects.

Why does my site show a 500 error after uploading .htaccess?

Apache found a line it cannot use, often a module that is not installed or a directive your host does not allow in .htaccess. Restore the previous file, then add sections back one at a time.

Should I use a 301 or a 302 redirect?

Use 301 when a page has moved for good, such as in a migration. Use 302 or 307 only for temporary moves, because search engines keep the old URL indexed.

How do I avoid redirect chains?

Point every old URL straight at its final destination. The generator places redirects above the HTTPS and www rules and warns when a target redirects again.

Can I keep my existing custom rules?

Yes. Paste them into Custom rules and they are added at the end of the file unchanged. The lint checks that RewriteEngine On comes before them.