.htaccess Rewrite Rule & Redirect Generator

Generate Apache mod_rewrite rules: build your own RewriteRule with RewriteCond conditions, flags and a path tester, or use the quick presets for HTTPS upgrades, www canonicalization, single URL moves and domain migrations. Always test on staging, because a wrong rewrite rule can take a site down.

Runs in browser

Start from a preset

Sends everything under /old-folder/ to /new-folder/ and keeps the rest of the path.

Rule 1

Only when… (optional conditions, all must pass unless OR is ticked)

No conditions: the rule applies whenever the pattern matches.

Match: 301 redirect to /new-folder/guides/setup.

1 rule, written for a .htaccess file in your site's root folder on Apache with mod_rewrite enabled. Apache runs the rules top to bottom, so put the most specific ones first. Conditions are not evaluated by the path tester below because they depend on your server's files and request headers.

RewriteEngine On

# Move /old-folder/... to /new-folder/...
RewriteRule ^old-folder/(.*)$ /new-folder/$1 [R=301,L]

Paste into the .htaccess file in your site's root folder (keep a backup first). It works on Apache with mod_rewrite enabled and AllowOverride permitting it. Nginx and IIS use different syntax.

How to use this tool

Write your own RewriteRule in the Rewrite rule builder (with conditions, flags and a path tester), or use Quick redirects for the common cases. Then paste the result into the .htaccess file in your site's root. For Apache servers.

  1. Pick a mode

    The Rewrite rule builder tab writes your own RewriteRule lines, with optional RewriteCond conditions. The Quick redirects tab covers the one-step cases: HTTP to HTTPS, HTTPS to HTTP, WWW to apex, apex to WWW, a single path to a URL, a full domain migration, or adding or removing the trailing slash.

  2. Build a rule

    In the Rewrite rule builder, start from a preset such as Hide .php extensions or Move a folder, or from Blank rule. Edit the URL pattern, Destination and What should happen, add conditions to limit when a rule applies, and use Add another rule for more. The rules update as you type.

  3. Check a rule with Try a path

    Type a sample path such as old-folder/page under Try a path to see whether the rule matches and where it would send the visitor. Conditions are not evaluated there. The warning box flags common mistakes such as a leading slash in the pattern or a destination that can loop.

  4. Or use a quick redirect

    On the Quick redirects tab, choose a Redirect type. The form changes with the type: redirecting a domain asks for the Apex domain; a single page asks for the Source path or URL and the Destination URL; a migration asks for the Old site URL and the New site base URL. Then click Generate .htaccess rules.

  5. Back up, then paste

    Copy the rules (or download them) and paste them near the top of the .htaccess file in your site's root folder, after saving a copy of the existing file.

  6. Test before you trust it

    Load the old address in a private window and confirm it lands on the right place in a single hop with no loop. curl -I shows the status code and Location header.

Good to know: A mistake in .htaccess can take a whole site offline with a 500 error. Keep a backup of the working file so you can restore it in seconds.

Redirects that preserve rankings instead of losing them

An .htaccess redirect and rewrite rule generator produces Apache mod_rewrite rules — the server-level instructions that send a request for one URL to another, or quietly serve it from a different file, with a status code telling browsers and search engines whether a move is permanent.

Redirects are where site migrations succeed or quietly fail. Done well, a 301 passes the accumulated ranking signals of the old URL to the new one and nobody notices the change. Done badly, you get chains that dilute signals, loops that make the site unreachable, or a blanket redirect to the homepage that throws away every deep link you had.

This generator covers the patterns that account for most real redirect work: forcing HTTPS, canonicalising www, moving single URLs, migrating a domain, and normalising trailing slashes. For everything else, the Rewrite rule builder writes RewriteRule and RewriteCond lines from presets such as hiding .php extensions, moving a folder or returning 410 Gone, and a path tester shows what a rule does to a sample URL. This page explains what each rule does line by line, and how to deploy without taking the site down.

Choosing the status code

Direct answer: use 301 for anything permanent. The other codes exist for specific situations that are rarer than people assume.

301 Moved Permanently
The default for migrations and canonicalisation. Search engines consolidate signals to the target, and browsers cache it — which is exactly why a mistaken 301 is painful to undo.
302 Found
Temporary. The original URL keeps its ranking signals. Correct for A/B tests, maintenance pages and geo-routing you intend to reverse.
307 and 308
The strict equivalents of 302 and 301 that guarantee the HTTP method is preserved. Matters for POST requests to API endpoints; rarely relevant to page redirects.
The R and L flags
R=301 sets the status code; L stops rule processing so later rules do not also fire. Omitting L is a common cause of unexpected double redirects.
RewriteCond binds to one rule only
A condition applies to the single RewriteRule immediately following it. Adding a rule in between silently changes which condition guards what.
Where the file lives
Rules go in the .htaccess file at your document root, and Apache needs AllowOverride enabled for them to take effect. If nothing happens after uploading, this is the first thing to check.

Reading a generated rule

Copying a rule you cannot read is how loops happen. Here is the HTTPS rule, line by line.

Example 1: Force HTTPS

Generated rule
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Line by line
1. Turn the rewrite engine on
2. Only when the request is NOT already HTTPS
3. Send it to the same host and path over
   https, permanently, and stop processing

The condition on line 2 is what prevents an infinite loop. A rule without it redirects HTTPS requests to HTTPS forever, and the browser gives up with ERR_TOO_MANY_REDIRECTS.

Example 2: Canonicalise www in one hop

Generated rule
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]
Why it is one hop
http://www → https://apex   (1 redirect)
not  http://www → https://www → https://apex

Because the target specifies both the scheme and the host, the HTTPS and www changes happen together. Ordering the two rules separately produces a two-hop chain on every request.

Example 3: Hide .php extensions

Generated rules
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
What each half does
/about.php → 301 → /about
   visitors and crawlers see one address
/about → served from about.php
   the address bar does not change

The first rule tests THE_REQUEST, the original request line, so it only fires when someone asked for the .php address. An internal rewrite does not change THE_REQUEST, which is what stops the pair of rules from looping.

Example 4: The migration mistake worth avoiding

What people do
RedirectMatch 301 ^/(.*)$ https://newsite.com/
What it costs
Every old URL → the new homepage.
Google treats these as soft 404s.
Deep-link ranking signals are lost.

Map old URLs to their genuine equivalents. A blanket redirect to the homepage is the most common reason traffic never recovers after a domain move.

Writing your own RewriteRule

The Rewrite rule builder writes one RewriteRule per rule card, with optional RewriteCond lines above it. These are the parts you control.

Pattern
A regular expression matched against the path without its leading slash when the rules live in .htaccess. ^old-page$ matches /old-page exactly. ^/old-page$ never matches, which is the most common reason a rule seems to do nothing.
Destination and $1
Parentheses in the pattern capture text, and $1, $2 insert it into the destination. ^old-folder/(.*)$ with /new-folder/$1 moves a whole folder and keeps the rest of the path.
L versus END
L stops the current pass, but in .htaccess Apache then re-runs the rules on the rewritten URL, which can trigger earlier rules again. END, available from Apache 2.3.9, stops all rewriting for the request.
QSA and QSD
By default the original query string is carried over when the destination has none of its own. QSA adds it to a destination that already has a ?query, and QSD discards it.
NC and NE
NC makes the pattern case-insensitive. NE stops Apache escaping special characters in the destination, which matters when the destination already contains encoded characters such as %20.
Conditions and OR
A RewriteCond tests a server variable such as the host, the query string or whether the requested file exists (-f, -d). Conditions must all pass unless OR is set, and they guard only the one RewriteRule that follows them.
410 Gone
For content you removed on purpose, a 410 says it is gone and not coming back. Google treats 404 and 410 very similarly, but 410 states the intent unambiguously.
What the path tester cannot do
Try a path runs your pattern against a sample URL and shows the resulting destination. It does not evaluate conditions, because those depend on the files on your server and on request headers, so always confirm on a staging copy.

Deploying without taking the site down

A syntax error in .htaccess returns 500 for every request on the site, not just the redirected ones. Treat it as production code.

Back up the current file first

Download the existing .htaccess before editing. Being able to restore it in ten seconds is the difference between a hiccup and an outage.

Put rewrite rules near the top

Rules run in order, and CMS platforms append their own front-controller blocks. Placing redirects after a catch-all WordPress block means they never fire.

Test with curl, not a browser

Browsers cache 301s aggressively — you may be seeing yesterday’s redirect. curl -I shows the real status and Location header, and a private window is only a partial substitute.

Avoid chains longer than one hop

Each additional hop adds latency and risk. Audit the full chain after any change: old URL → final URL should be a single 301.

.htaccess is read on every request

Apache re-reads it for every request in every directory along the path. On a busy site, moving rules into the virtual host configuration is measurably faster — and .htaccess is only for shared hosting where you cannot.

Nginx does not read .htaccess at all

These rules only work on Apache. On Nginx, LiteSpeed, Caddy or a CDN edge worker, the equivalent has to be written in that platform’s own syntax. Confirm what your host actually runs before spending an hour debugging a file nobody reads.

Behind Cloudflare or a load balancer, “HTTPS” is not what Apache sees

When a proxy terminates HTTPS in front of Apache, Apache receives plain HTTP, so a rule that redirects whenever HTTPS is off fires on every request and ends in ERR_TOO_MANY_REDIRECTS. Cloudflare’s Flexible SSL mode is the classic case. Either set the proxy to talk HTTPS to your server (Cloudflare’s Full mode or higher), or use the proxy option in Quick redirects, which also checks the X-Forwarded-Proto header the proxy sets to the visitor’s original protocol.

A migration redirect checklist

  • Export every indexed URL from Search Console and your analytics before the move — not after.
  • Map each old URL to its closest equivalent; use the homepage only where genuinely nothing corresponds.
  • Keep redirects live for at least a year. Search engines revisit old URLs long after the move.
  • Update internal links to point at final destinations so users never traverse a redirect.
  • Confirm the new host serves a valid certificate for every hostname you redirect to, using the SSL certificate checker.
  • Verify the DNS records for both domains with the DNS checker before switching traffic.
  • Re-check that crawl rules on the new domain are correct with the robots.txt tester.

Sources and further reading

Where the technical statements on this page come from: standards, specifications and the official documentation of the tools and services involved.

How this page is maintained

The rules quoted on this page are the exact output of this generator, and the flag and condition behaviour follows Apache’s mod_rewrite documentation. The warning that .htaccess has no effect on Nginx-class servers is included because it is the most common reason a correct rule appears to do nothing.

Written and reviewed by the ToolsRacks editorial team. Last reviewed: . Found an error? Tell us — see our editorial standards.

Works great with these tools

Redirects to HTTPS only help if the certificate is valid — verify with our free SSL certificate checker.

After changing hosts for a migration, confirm A/AAAA records with our free DNS checker.

Avoid redirect loops on paths meant to stay crawlable — test paths with our free robots.txt generator.

Frequently asked questions

Answers to the questions people ask most often about .htaccess Redirect Generator.

Should I use a 301 or a 302 redirect?

Use 301 for permanent moves so search engines consolidate ranking signals to the new URL. Use 302 only for genuinely temporary situations such as A/B tests or maintenance pages, where the original URL should keep its signals.

What causes a redirect loop?

A rule whose condition does not exclude the destination. An HTTPS rule without the RewriteCond that checks the request is not already HTTPS will redirect HTTPS to HTTPS forever, and the browser gives up.

Why does my HTTPS redirect cause ERR_TOO_MANY_REDIRECTS behind Cloudflare?

With Cloudflare’s Flexible SSL mode, Cloudflare connects to your server over plain HTTP, so an .htaccess rule that redirects every HTTP request to HTTPS fires again on each request and the browser reports ERR_TOO_MANY_REDIRECTS. Cloudflare’s own advice is to remove the HTTPS redirect from your server or switch the SSL/TLS mode to Full or higher, which needs a certificate on your server. To keep the rule, tick the Cloudflare/CDN option under Quick redirects so it also checks the X-Forwarded-Proto header, which Cloudflare sets to the protocol the visitor used.

Why is my .htaccess file having no effect?

Three usual causes: the server is not Apache, in which case .htaccess is never read; AllowOverride is not enabled; or the rules were placed after a CMS front-controller block that catches the request first.

Can I redirect an entire old domain to a new one?

Yes, but map old URLs to their genuine equivalents rather than sending everything to the homepage. A blanket homepage redirect is treated as a soft 404 and is the most common reason traffic never recovers after a migration.

How do I test a redirect properly?

Use curl -I to see the actual status code and Location header. Browsers cache 301 responses aggressively, so what you see in a browser may be yesterday’s redirect rather than the current rule.

How long should I keep redirects in place?

At least a year. Search engines revisit old URLs long after a move, and inbound links from other sites can persist indefinitely. Update your own internal links so users never traverse a redirect.

How do I write a RewriteRule?

A RewriteRule has a pattern, a destination and optional flags, as in RewriteRule ^old-page$ /new-page [R=301,L]. In a .htaccess file the pattern is a regular expression matched against the path without its leading slash. Use the Rewrite rule builder tab in the generator to assemble the line from fields and see what it does to a sample path.

What is the difference between a rewrite and a redirect?

A redirect tells the browser to request a different address, so the address bar changes and a status code such as 301 is returned. An internal rewrite serves the content of a different file or script while the address bar stays the same, which is how clean URLs such as /about are served from about.php.

How do I remove .php from my URLs?

Open the Rewrite rule builder tab and load the Hide .php extensions preset. It adds one rule that serves /about from about.php and one that redirects direct requests for /about.php to /about, so each page has a single address. Then update your internal links to the extensionless addresses.

What is the difference between the L and END flags?

L stops the current pass through the rules, but in a .htaccess file Apache then re-runs the rules on the rewritten URL, which can trigger earlier rules again. END, available from Apache 2.3.9, stops all rewrite processing for the request.

Guides for the .htaccess Redirect Generator

Troubleshooting and background reading written for people who use this tool.

Picked from SEO & Web Tools and adjacent categories. Each one has its own page explaining how it works and where it falls short.

Looking for something else? See every tool in SEO & Web Tools → · Browse all 32 free tools →