Starlight Tools

.htaccess Redirect Generator & RewriteRule Tester

Generate single or bulk Apache redirects, or paste and test an existing .htaccess configuration. Detect malformed rules, chains, and loops, then copy a ready-to-use file free in your browser—no signup or upload.

Redirects

Start with the old URL and destination. Output updates live.
Redirect presets

Add an editable, loop-guarded recipe. Replace example hosts and paths before deployment.

Bulk paste or CSV/TSV import

Columns: old URL, destination, optional status. Headers are optional; quoted CSV is supported.

Advanced settings
Resolves path-only tests and checks same-site chains.

Redirect/RedirectMatch use URL-path matching and run before per-directory RewriteRule processing. Conditions, query controls, case matching, and fragment handling require mod_rewrite; mixing both styles can make ordering surprising.

Private by design: generation, imports, and testing run locally. No request is sent to your website.

Redirect test matrix

Tests the valid generated rules above. One request per line; optional expected status and destination may be separated by tabs, commas, or |.

Stops chains and detects loops.

Common redirect examples

Each worked example is visible without JavaScript. “Load into generator” makes an editable copy.

Single page

Move exactly one page.

RedirectMatch 301 "^/old-page$" "/new-page"

/old-page/new-page

Directory wildcard

Retain the remainder of every path below a folder.

Redirect 301 "/docs" "/guides"

/docs/start/guides/start

Domain migration

Move all paths from an old host.

RewriteCond %{HTTP_HOST} ^old\.example$ [NC]
RewriteRule ^(.*)$ https://new.example/$1 [R=301,L]

old.example/anew.example/a

Force HTTPS

Redirect only requests that are not already secure.

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

http://example.com/ahttps://example.com/a

www canonicalisation

Remove www without looping.

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

www.example.com/aexample.com/a

Add trailing slash

Add a slash to paths that do not end with one.

RewriteRule ^(.+[^/])$ /$1/ [R=301,L]

/about/about/

Query parameter

Match one exact incoming query and remove it.

RewriteCond %{QUERY_STRING} ^campaign=summer$
RewriteRule ^offer$ /sale [R=302,L,QSD]

/offer?campaign=summer/sale

Remove extension

Turn legacy .html paths into clean URLs.

RewriteRule ^(.+)\.html$ /$1 [R=301,L]

/about.html/about

.htaccess redirect troubleshooting

Redirect vs internal rewrite

[R] sends a browser redirect; omitting it internally maps a request. This tester supports redirecting rules only.

RewriteRule ^old$ /new [R=301,L]

/old → HTTP 301 /new

Why ^/ fails

A root .htaccess RewriteRule sees a path without its leading slash.

RewriteRule ^old$ /new [R=301,L]

/old matches ^old$

ERR_TOO_MANY_REDIRECTS

The destination is matching the same canonicalisation rule. Guard the scheme or host first.

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]

HTTPS requests skip the rule.

WordPress placement

Put external redirects before the WordPress front controller so index.php does not consume the request.

RedirectMatch 301 ^/old$ /new
# BEGIN WordPress

/old redirects before CMS routing.

HTTP 500 recovery

Restore a backup, then add directives back in small groups. A malformed flag or unsupported directive can invalidate the file.

# Temporarily comment the suspect line

Server returns normally after rollback.

Proxy or CDN HTTPS

Your origin may see HTTP while the visitor used HTTPS. Use the trusted proxy’s documented forwarded-protocol setup to avoid loops.

RewriteCond %{HTTP:X-Forwarded-Proto} !https

Only use a header your proxy overwrites.

Query-string matching

RewriteRule never matches the query. Test QUERY_STRING separately and use QSD to remove it.

RewriteCond %{QUERY_STRING} (^|&)id=42(&|$)
RewriteRule ^item$ /answer [R=302,L,QSD]

/item?id=42/answer

Case and server compatibility

[NC] makes a rewrite match case-insensitively. These rules target Apache 2.4 and generally LiteSpeed; Nginx does not read .htaccess.

RewriteRule ^old$ /new [R=301,L,NC]

/OLD/new

Choose the right redirect status

StatusMeaningMethod behaviorTypical use
301 Moved PermanentlyPermanent moveOlder clients may change a non-GET request to GETStable page or site migrations after testing
302 FoundTemporary redirectOlder clients may change a non-GET request to GETShort-lived routing and pre-production testing
303 See OtherFetch another resourceFollow-up request uses GET (or HEAD)Post/Redirect/Get workflows
307 Temporary RedirectTemporary redirectPreserves the request method and bodyTemporary API or form endpoint moves
308 Permanent RedirectPermanent movePreserves the request method and bodyPermanent API or method-sensitive moves

How to generate and deploy redirect rules

  1. Add rules in priority order. Put specific paths above broader prefixes. Use advanced regex only when exact or prefix matching cannot express the redirect.
  2. Choose a temporary or permanent status. A 302 or 307 is easier to change while testing; switch to 301 or 308 only when the destination is stable.
  3. Review query-string behavior. Apache normally preserves an incoming query when the destination has none. Use QSA to append it to a destination query or QSD to discard it.
  4. Test representative URLs. Include exact paths, descendants, near misses, query strings, mixed case, and destinations that might be caught again.
  5. Back up and deploy. Merge the output near the top of the root .htaccess, before CMS or front-controller rules. Do not overwrite unrelated security, caching, or routing directives.
  6. Verify the real response. Use browser developer tools or curl -I to confirm the status and Location. Check several redirects and watch for loops or unexpected chains.
Deployment limit: a syntax error can cause an HTTP 500 response. Your host must allow FileInfo overrides and enable mod_rewrite. Keep a recoverable backup and use your hosting provider’s staging or validation tools when available.

Scope, references, and verification

Root .htaccess context

The generated RewriteRule patterns omit the leading slash because Apache strips it in per-directory context.

Ordered trace

The simulator evaluates conditions and redirects from top to bottom, reports skipped rules, and follows the first redirect through a configurable number of hops.

Query strings are separate

The rule pattern matches the URL path, not its query string. Query behavior is controlled with the substitution and QSA or QSD.

Regex simulation has limits

Apache 2.4 uses PCRE, while this browser uses JavaScript regular expressions. Basic anchors, groups, classes, and common quantifiers align; engine-specific features may not.

Last verified: 31 August 2026 · Target: Apache HTTP Server 2.4, root .htaccess (per-directory) context · Publisher and technical review: Starlight Robotics engineering.

Testing method: maintained simulation fixtures cover exact, prefix, regular-expression, host, scheme, query, duplicate, conflict, chain, and loop cases. The simulator does not claim byte-for-byte parity with Apache PCRE or a live server.

Primary references: Apache 2.4 mod_rewrite directives, Apache 2.4 rewriting guide, Apache 2.4 mod_alias redirects, and RewriteRule flags.

.htaccess redirect FAQ

What is the difference between a 301 and 302 redirect?

A 301 says the move is permanent, while a 302 says it is temporary. Browsers and search engines may cache permanent redirects more aggressively, so test with a temporary status before making a migration permanent.

Should I use 307 or 308 for form URLs?

307 and 308 explicitly preserve the request method and body: 307 is temporary and 308 is permanent. Redirecting POST or other non-GET requests still requires application-specific testing.

Why does a RewriteRule pattern omit the leading slash?

In per-directory .htaccess context, Apache removes the directory prefix, including the leading slash, before matching a RewriteRule pattern. This generator targets a root .htaccess file.

Are incoming query strings preserved?

By default, Apache carries the incoming query string forward when the substitution has no query string. A query in the substitution replaces it. QSA appends the incoming query, and QSD discards it.

Does the browser tester exactly reproduce Apache?

No. It models generated rules and the explicitly supported pasted directives in common root .htaccess behavior, but JavaScript and Apache PCRE regular expressions differ. Modules, server configuration, filesystem mapping, encoded paths, and rules elsewhere can change the real result.

Where should these rules go?

They are designed for the site root .htaccess and generally belong before front-controller rules. The server must allow FileInfo overrides and have mod_rewrite enabled.

Can redirects create a loop?

Yes. A destination that is also matched by the same or a later redirect can loop or chain. This tool flags obvious self-redirects, but you should test every representative URL on the deployed server.

Explore more tools