Apache’s RedirectMatch directive is one of the simplest ways to redirect URLs with pattern matching in an .htaccess file. If you have moved pages, renamed folders, retired old content, or consolidated duplicate URLs, RedirectMatch can handle many redirect jobs with a single clean rule.
TLDR: RedirectMatch uses regular expressions to match old URL paths and send visitors to new ones, usually with a 301 permanent redirect. For example, RedirectMatch 301 ^/old/(.*)$ /new/$1 sends everything from /old/ to the matching path under /new/. In a real site migration, replacing 120 individual redirects with 6 RedirectMatch rules can reduce maintenance time by more than 80% while preserving SEO signals and preventing broken links.
What Is RedirectMatch?
RedirectMatch is part of Apache’s mod_alias module. Unlike the basic Redirect directive, which matches a fixed path, RedirectMatch uses a regular expression. That means you can match many similar URLs with one flexible rule.
It is commonly used for:
- Redirecting an entire directory
- Changing file extensions, such as
.htmlto extensionless URLs - Moving blog posts into a new structure
- Removing outdated sections of a website
- Consolidating duplicate URL patterns
Because it is processed by Apache before the request reaches many application layers, it can be fast and efficient. However, it should be used carefully: one broad pattern can accidentally redirect more than intended.
Image not found in postmetaBasic RedirectMatch Syntax
The general syntax is:
RedirectMatch [status] regex target
Here is what each part means:
- status: The HTTP status code, such as
301,302,307, or410. - regex: The regular expression used to match the requested URL path.
- target: The destination URL or path.
A simple example:
RedirectMatch 301 ^/old-page$ /new-page
This redirects /old-page to /new-page. The ^ means “start of the path,” and $ means “end of the path.” These anchors are important because they prevent partial matching. Without them, a rule might match more URLs than expected.
Choosing the Right Redirect Status
The status code tells browsers and search engines how to treat the redirect:
- 301 Permanent: Use when a page has moved permanently. This is the most common SEO-friendly redirect.
- 302 Found: Use for temporary redirects, such as short campaigns or testing.
- 307 Temporary: Similar to 302, but preserves the request method more strictly.
- 410 Gone: Use when content has been intentionally removed and there is no replacement.
For most site migrations, 301 is the right choice. It helps search engines transfer ranking signals from the old URL to the new one.
Common RedirectMatch Examples
1. Redirect an Entire Folder
If you moved a complete section from /products/ to /shop/, use a capture group:
RedirectMatch 301 ^/products/(.*)$ /shop/$1
The (.*) captures everything after /products/, and $1 reuses it in the destination. So /products/shoes/running becomes /shop/shoes/running.
2. Redirect Old HTML Pages to New Clean URLs
If your site removed .html extensions, this rule can redirect matching files:
RedirectMatch 301 ^/(.*)\.html$ /$1
For example, /about.html redirects to /about. The backslash before the dot is necessary because a plain dot in regex means “any character.”
3. Redirect a Specific Group of Pages
You can match only certain names using parentheses and the pipe symbol:
RedirectMatch 301 ^/(pricing|plans|packages)$ /pricing
This sends /pricing, /plans, and /packages to the single canonical URL /pricing.
4. Redirect Old Blog Dates to a New Blog Folder
If an old blog used dates in the URL and the new site uses a simpler structure, you might use:
RedirectMatch 301 ^/blog/[0-9]{4}/[0-9]{2}/(.*)$ /blog/$1
This matches URLs like /blog/2023/09/my-post and redirects them to /blog/my-post. The [0-9]{4} part matches a four-digit year, while [0-9]{2} matches a two-digit month.
5. Mark Removed Content as Gone
If an outdated section should not redirect anywhere, return a 410 Gone response:
RedirectMatch 410 ^/old-news/.*$
This tells search engines the content was deliberately removed. It can be better than redirecting irrelevant pages to the homepage, which often creates a poor user experience.
Important Regex Characters to Know
You do not need to be a regex expert to use RedirectMatch, but a few symbols are essential:
^matches the beginning of the URL path.$matches the end of the URL path..matches any single character..*matches any sequence of characters.( )creates a capture group.$1,$2, and so on reuse captured values in the target.|means “or.”\escapes special characters, such as dots.
The biggest beginner mistake is forgetting that regex can be broad. For instance, /old may match /older-page unless you use anchors such as ^/old$.
RedirectMatch vs RewriteRule
RedirectMatch is excellent for simple path-based redirects. However, it does not evaluate complex conditions in the same way Apache’s RewriteRule and RewriteCond do.
Use RedirectMatch when:
- You are redirecting based mainly on the URL path.
- You want readable, compact rules.
- You need straightforward permanent or temporary redirects.
Use RewriteRule when:
- You need to redirect based on the domain, query string, or protocol.
- You are forcing HTTPS or canonical hostnames.
- You need advanced conditional logic.
For example, redirecting all non-www traffic to www is usually better handled with RewriteCond, because RedirectMatch focuses on the path rather than the hostname.
Best Practices for Safe Redirect Rules
- Test on staging first: A single misplaced pattern can cause redirect loops or block important pages.
- Use 301 only when you are sure: Browsers cache permanent redirects aggressively.
- Place specific rules before broad rules: More specific matches should usually appear higher in the file.
- Avoid redirect chains: Send old URLs directly to their final destination.
- Monitor after launch: Check server logs, analytics, and crawl reports for unexpected 404s or loops.
A practical migration workflow is to export your top landing pages from analytics, map each old URL to a new one, then create RedirectMatch patterns for repeatable structures. If 65% of your old URLs share the same folder pattern, one rule may cover most of the migration.
Final Thoughts
RedirectMatch is a powerful middle ground between basic redirects and more complex rewrite logic. It keeps .htaccess files cleaner, reduces repetitive rules, and helps preserve user experience during site changes. The key is precision: use anchors, test your expressions, and make sure each redirect has a clear destination. With careful planning, RedirectMatch can turn a messy URL migration into a controlled, search-friendly process.