.htaccess redirects: rules, flags and the mistakes that break them
This .htaccess redirect generator writes Apache 301 and 302 rules for URL lists, HTTPS, www, domain moves and regex rewrites — with anchored patterns, loop detection and a tester that shows which rule wins. Below: how the two redirect modules differ, what each flag does, and why a rule that looks right does nothing.
What .htaccess actually is
.htaccess is a per-directory Apache configuration file. Apache reads it on every request, for every directory along the requested path, and applies whatever it finds — no restart needed.
That convenience is also its cost. Server-level config is parsed once at startup; .htaccess is parsed on every single hit. On a busy site the difference is measurable, which is why the standard advice is to develop rules in .htaccess and then move them into the vhost with AllowOverride None.
It only works if the server permits it. If AllowOverride is set to None for your directory, the file is ignored completely and silently — no error, no log entry, just a redirect that never happens.
mod_alias and mod_rewrite
Apache has two redirect systems. Knowing which you are using is most of the battle.
| mod_alias | mod_rewrite | |
|---|---|---|
| Directives | Redirect, RedirectMatch | RewriteRule, RewriteCond |
| Matching | Path prefix, or regex with RedirectMatch | Full regex |
| Conditions | None | RewriteCond on host, protocol, query, headers |
| Query string | Preserved automatically | Only with the QSA flag |
| Leading slash | Present | Stripped in .htaccess, present in vhost |
| Speed | Slightly faster | Negligibly slower |
| Use for | Simple one-to-one moves | HTTPS, www, domains, anything conditional |
mod_alias is applied first regardless of where the directives sit in the file. A RewriteRule above a Redirect for the same path will never fire, which reads as "Apache is ignoring my rule".Redirect matches a prefix, not a page
This is the single most damaging misunderstanding in Apache redirects, because the broken behaviour looks correct in testing.
Redirect 301 /about /new-about does not redirect the page /about. It redirects everything beginning with /about:
/about→/new-about— as intended./about-us→/new-about-us— not intended./about/team→/new-about/team— not intended./about.html→/new-about.html— usually a 404.
You test /about, it works, you ship it, and three unrelated pages break. The fix is an anchored RedirectMatch: RedirectMatch 301 ^/about$ /new-about. The ^ and $ make the match exact, and this generator always emits them.
The flags that matter
| Flag | Effect | When you need it |
|---|---|---|
R=301 | Send an external redirect with this status. | Always, for a redirect. Without R the rewrite is internal and the URL bar never changes. |
L | Stop processing further rules. | Almost always. Omitting it lets later rules rewrite your result again. |
QSA | Append the incoming query string. | Whenever campaign or tracking parameters must survive. |
QSD | Discard the incoming query string. | When a legacy parameter must not reach the new URL. |
NC | Case-insensitive matching. | After migrating from Windows or IIS hosting. |
NE | Do not escape special characters in the target. | When the destination contains # or a literal ?. |
G | Return 410 Gone. | Removing a page permanently, faster than waiting on 404s. |
$N and %N are not the same. $1 is a capture from the RewriteRule pattern; %1 is a capture from the preceding RewriteCond. Host-based rules need %1, and using $1 there is why so many copied www rules redirect to the wrong place.Forcing HTTPS behind a proxy
The textbook rule is RewriteCond %{HTTPS} off then RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]. On a plain server it is correct.
Behind Cloudflare, a load balancer, or most managed and cloud hosting, it produces an infinite loop. TLS terminates at the proxy, so Apache sees plain HTTP on the internal hop, redirects to HTTPS, the proxy forwards HTTP again, and the browser gives up with ERR_TOO_MANY_REDIRECTS.
The fix is to test the header the proxy sets instead:
RewriteCond %{HTTP:X-Forwarded-Proto} !https— the common convention.- On some hosts,
%{HTTP:CF-Visitor}or a provider-specific variable. - Best of all, terminate the redirect at the proxy or CDN so the request never reaches Apache.
Tick “Behind a proxy or load balancer” and the generator emits the header form.
Why a rule silently does nothing
In rough order of how often each is the culprit:
AllowOverrideis None. The whole file is ignored. Nothing is logged. Check the vhost, not the file.- A leading slash in the pattern. In
.htaccessthe path is matched without it, so^/blog/can never match. Examples copied from vhost config always have this. - An earlier rule matched with
[L]. Processing stopped before reaching yours. Order matters absolutely. - The browser cached an old 301. Your rule is fine; you are not reaching the server. Test in a private window or with
curl -I. - mod_alias is shadowing it. A
Redirectanywhere in the file beats everyRewriteRulefor the same path. - An unescaped dot.
.matches any character, so^page.html$also matchespageXhtml— and more importantly your intended literal may be matched by an earlier, broader rule. - The file has Windows line endings or a BOM. Apache can reject the first directive outright, sometimes with a 500.
.htaccess takes down the entire directory with a 500 — every page, immediately. Always keep a known-good copy, and wrap rules in <IfModule> so a missing module degrades to “no redirect” rather than “site offline”.Choosing the status code
301 Moved Permanently
The default for a real move. Cached hard by browsers, sometimes forever — which is why you deploy with 302 first and promote it once confirmed.
302 Found
Temporary. Search engines keep the original indexed. Correct for maintenance pages, tests and geolocation.
308 / 307
Permanent and temporary equivalents that preserve the request method and body. Use these for API endpoints and anything receiving POST.
410 Gone
No destination — the resource is deliberately removed. Search engines drop it faster than a 404.
Related server and SEO tools
- Nginx Redirect Generator — the same migration expressed as Nginx rules and maps.
- Canonical Tag Generator — consolidate duplicates you cannot redirect.
- X-Robots-Tag Generator — indexing headers for files and directories.
- Meta Robots Tag Generator — per-page indexing control.
- Sitemap Index Generator — resubmit clean URLs after a migration.