Skip to content

Commit 7ffa3ed

Browse files
Improve redirects documentation with advanced regex patterns
- Add named capture group examples with regex patterns - Document Path-to-RegExp syntax including {0} delimiter - Include real-world examples (release notes, versioned APIs) - Add pattern syntax reference table - Based on user feedback about complex regex redirects Co-Authored-By: [email protected] <[email protected]>
1 parent 78f748f commit 7ffa3ed

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

fern/snippets/redirects.mdx

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The `redirects` object allows you to redirect traffic from one path to another. You can redirect exact paths or use dynamic patterns with [`regex`](https://www.npmjs.com/package/path-to-regexp) parameters like `:slug` to handle bulk redirects. You can redirect to internal paths within your site or external URLs.
1+
The `redirects` object allows you to redirect traffic from one path to another. You can redirect exact paths or use dynamic patterns with [Path-to-RegExp](https://www.npmjs.com/package/path-to-regexp) parameters like `:slug` to handle bulk redirects. You can redirect to internal paths within your site or external URLs.
22

33
If your docs are hosted on a subpath (like `buildwithfern.com/learn`), include the subpath in both the source and destination paths.
44

@@ -17,18 +17,52 @@ If your docs are hosted on a subpath (like `buildwithfern.com/learn`), include t
1717
destination: "/new-location"
1818
permanent: false # Use 307 (temporary) instead of 308 (permanent)
1919

20-
# Regex-based redirects
20+
# Pattern-based redirects
2121
- source: "/old-folder/:slug" # Matches single segments: /old-folder/foo
2222
destination: "/new-folder/:slug"
2323
- source: "/old-folder/:slug*" # Matches multiple segments: /old-folder/foo/bar/baz
24-
destination: "/new-folder/:slug*"
24+
destination: "/new-folder/:slug*"
25+
26+
# Named capture groups with regex patterns
27+
- source: "/release-notes/:year(\\d{4})-{0}:month(\\d{1,2})"
28+
destination: "/docs/release-notes/:year/:month/1"
29+
- source: "/api/v:version(\\d+)/:endpoint*"
30+
destination: "/api-reference/:version/:endpoint*"
2531
```
2632
</CodeBlock>
2733
2834
<Info>
2935
Parameters suffixed with an asterisk (`*`) match zero or more path segments, capturing everything that follows in the URL. Use this when redirecting entire folder structures while preserving nested paths.
3036
</Info>
3137

38+
### Advanced patterns
39+
40+
Fern uses [Path-to-RegExp](https://github.com/pillarjs/path-to-regexp) for pattern matching. Named capture groups can be defined with regex patterns and reused in destination URLs:
41+
42+
<CodeBlock title="docs.yml">
43+
```yml
44+
redirects:
45+
# Match 4-digit years
46+
- source: "/blog/:year(\\d{4})/:slug*"
47+
destination: "/archive/:year/:slug*"
48+
49+
# Match dates with literal separator
50+
- source: "/posts/:year(\\d{4})-{0}:month(\\d{2})-{0}:day(\\d{2})"
51+
destination: "/blog/:year/:month/:day"
52+
53+
# Match numeric IDs only
54+
- source: "/user/:id(\\d+)"
55+
destination: "/users/:id/profile"
56+
```
57+
</CodeBlock>
58+
59+
**Pattern syntax:**
60+
- `:name` - Captures a single path segment
61+
- `:name*` - Captures zero or more segments
62+
- `:name(\\d+)` - Captures digits only
63+
- `:name(\\d{4})` - Captures exactly 4 digits
64+
- `{0}` - Literal character delimiter between parameters
65+
3266
<ParamField path="source" type="string" required={true}>
3367
The incoming request path pattern.
3468
</ParamField>

0 commit comments

Comments
 (0)