RedirectMatch Generator
Updated October 2026Build Apache RedirectMatch rules with regular expressions: move folders, change extensions, rewrite IDs or mark sections 410 Gone, with anchored patterns, loop warnings and a URL tester. Free, runs in your browser.
Advertisement
Advertisement
1. Start from a recipe
/old-blog/anything → /blog/anything.
2. The rule
Your RedirectMatch line
RedirectMatch 301 ^/old-blog/(.*)$ https://www.example.com/blog/$1What it does, line by line
- Pattern: the request path (with its leading slash, without the query string) must match ^/old-blog/(.*)$.
- Response: 301 permanent redirect to https://www.example.com/blog/$1, where $1, $2... are the parts captured by the parentheses. The original query string is appended automatically.
- Where: the .htaccess file in your site root (mod_alias is enabled on nearly every Apache host), above any RewriteRule block.
Test on a URL
Matches. Response: https://www.example.com/blog/my-first-post?utm_source=newsletter
What RedirectMatch Does (and How It Differs From Redirect)
RedirectMatch is the regular-expression version of Apache's Redirect directive. Both belong to mod_alias, which is enabled on practically every Apache host, so they work even where mod_rewrite is restricted. The difference is how the URL is matched:
| Directive | Matching | Example | Also matches |
|---|---|---|---|
| Redirect | Prefix: the path must start with the given string | Redirect 301 /old /new | /old/anything → /new/anything, and /old itself |
| RedirectMatch | Regex against the full path (with its leading slash) | RedirectMatch 301 ^/old$ /new | Only /old; sub-paths are untouched |
The prefix behaviour of Redirect is the source of two classic bugs: a loop when the new URL starts with the old one (Redirect /shop /shop-new sends /shop-new to /shop-new-new and so on), and unexpected matches such as Redirect /old /new also catching /old-news. An anchored RedirectMatch pattern avoids both, which is why the generator above always starts patterns with ^/ and warns when the end is not anchored.
RedirectMatch Syntax and Back-References
RedirectMatch [status] regex target-URL
RedirectMatch 301 ^/old-blog/(.*)$ https://www.example.com/blog/$1- status: 301 (permanent), 302 (temporary, the default), 303, 307, 308, or 410 (gone, no target). The words
permanent,temp,seeotherandgonealso work. - regex: a PCRE pattern tested against the URL path. The query string is not part of the path; it is appended to the target automatically.
- target: a full URL or an absolute path.
$1…$9insert the groups captured by parentheses in the regex.
Unlike RewriteRule, there are no flags: no NC for case-insensitivity (use the (?i) inline modifier, which the generator adds when you tick the option), no L, no QSA. If you need those, switch to the RewriteRule generator.
RedirectMatch Examples You Can Copy
Move a folder and keep the rest of the path
RedirectMatch 301 ^/old-blog/(.*)$ https://www.example.com/blog/$1Change every .php URL to .html
RedirectMatch 301 ^/(.+)\.php$ https://www.example.com/$1.htmlNumeric IDs to a new structure
RedirectMatch 301 ^/product/([0-9]+)/?$ https://www.example.com/shop/item-$1Strip index.html and index.php
RedirectMatch 301 ^/(.*/)?index\.(html?|php)$ https://www.example.com/$1Remove dates from WordPress permalinks
RedirectMatch 301 ^/[0-9]{4}/[0-9]{2}/([^/]+)/?$ https://www.example.com/blog/$1A whole section deleted on purpose
RedirectMatch 410 ^/forum(/.*)?$410 tells Google the content is gone for good; it drops the URLs faster than a 404 and does not pass visitors to an unrelated page. Use it instead of redirecting deleted content to the homepage, which Google treats as a soft 404.
Regex Reminders for RedirectMatch
| Pattern piece | Meaning |
|---|---|
| ^ and $ | Start and end of the path. Use both for exact matches. |
| \. | A literal dot. An unescaped . matches any character, so .php also matches -php. |
| (.*) | Capture anything, including slashes, as $1. |
| ([^/]+) | Capture one path segment (no slashes). |
| ([0-9]+) | Capture a number. |
| /?$ | Allow an optional trailing slash before the end. |
| (?i) | Case-insensitive matching, placed at the start of the pattern. |
| (/.*)?$ | The section itself plus anything beneath it. |
RedirectMatch or RewriteRule: Which One to Use
Use RedirectMatch when the decision depends only on the path and you want a single readable line with no module requirements. Use RewriteRule when the decision also depends on the host, the protocol, the query string or whether a file exists, when you need an internal rewrite (no visible redirect), or when you need flags. Avoid mixing both for the same URLs: mod_alias runs before mod_rewrite, and a Redirect that matches first will win over the rewrite rule below it, which produces confusing chains. The .htaccess tester shows which directive answers a given URL when both are present.
Where to Put It and How to Check It
- Add the line to the
.htaccessin your document root, above any# BEGIN WordPressor framework block. - Keep one redirect per line and order them from specific to general; mod_alias stops at the first match.
- Test an old URL in a private window or with the broken link checker: it should answer 301 with the new URL in the Location header, and the new URL should answer 200.
- Update internal links and the sitemap to the new URLs so the redirect only serves external traffic. The sitemap generator can crawl the live site for you.
- For a long list of one-to-one moves, the bulk redirect generator writes anchored RedirectMatch lines from a two-column paste.
How to Use
- 1
Pick a recipe (folder move, extension change, numeric IDs, strip index.html, date archives, 410 gone) or start from a custom regex
- 2
Adjust the pattern, the status code and the target; $1, $2 reuse the parts captured by parentheses
- 3
Read the warnings: missing anchors, a leading slash that is absent, a target that would match the pattern again
- 4
Paste an old URL into the tester to confirm where it goes
- 5
Copy the line into the .htaccess in your site root, above any RewriteRule or WordPress block
Why This Tool Matters for SEO
RedirectMatch is the simplest way to redirect a whole pattern of URLs on Apache, and the easiest to get subtly wrong: an unanchored regex that matches /old inside /blog/old-news, an unescaped dot, a target that matches the pattern again and loops, or a prefix Redirect used where an exact match was needed. Generating the line from a recipe, with the pattern anchored and tested on a real URL, avoids the mistakes that only show up after deployment.
Frequently Asked Questions
What is the difference between Redirect and RedirectMatch?
Redirect matches the beginning of the path as plain text: Redirect /old /new also sends /old/anything and /old-news. RedirectMatch takes a regular expression, so you control exactly what matches (^/old$ for one page, ^/old/(.*)$ for a folder) and can reuse captured parts in the target with $1. Both belong to mod_alias and work without mod_rewrite.
Does RedirectMatch keep the query string?
Yes. The regex is matched against the path only, and the original query string is appended to the target URL automatically. To drop it you need a RewriteRule with the QSD flag or a trailing ? in the substitution.
How do I make RedirectMatch case-insensitive?
RedirectMatch has no NC flag. Put the inline modifier (?i) at the start of the pattern, for example RedirectMatch 301 (?i)^/old-page$ /new-page. The generator adds it when you tick "ignore upper/lower case".
Why does my RedirectMatch cause a redirect loop?
Because the target URL also matches the pattern. RedirectMatch 301 ^/shop(.*)$ /shop-new$1 matches /shop-new too, redirecting it to /shop-new-new and so on. Anchor the pattern (^/shop(/.*)?$) or pick a target outside the pattern. The generator and its tester warn when the target matches again.
When should I use 410 instead of a redirect?
When the content is gone for good and there is no equivalent page. RedirectMatch 410 ^/forum(/.*)?$ tells search engines to drop the URLs quickly, and it is more honest than redirecting deleted content to the homepage, which Google treats as a soft 404.
Can I use RedirectMatch and RewriteRule in the same .htaccess?
Yes, but not for the same URLs. mod_alias (Redirect, RedirectMatch) is processed before mod_rewrite, so a matching RedirectMatch wins and the RewriteRule below never sees the request, which can create chains. Use one mechanism per set of URLs; the .htaccess tester on this site shows which directive answers a given URL.
Related Tools You Might Like
Meta Tags Generator
Generate optimized meta tags for your pages
Google SERP Preview
Preview how your page appears in Google search results. Optimize titles and descriptions for CTR
Robots.txt Generator
Create and validate robots.txt files for search engine crawlers
JSON-LD Schema Generator
Generate structured data (JSON-LD) for rich snippets
Related Guides
More in Technical SEO tools · All free SEO tools