Skip to content

Commit 3b9698d

Browse files
authored
Merge pull request #462 from ByteInternet/ddelwig-patch-3
Update how-to-use-nginx.md
2 parents 008a4d1 + 8ff8c5e commit 3b9698d

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

docs/hypernode-platform/nginx/how-to-use-nginx.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,48 @@ rewrite ^/fr/(.*)$ http://yourshop.fr/$1 permanent;
128128
This will also maintain subfolders and query strings (such as <http://yourshop.com/fr/subfolder?arguments>).
129129

130130
If the move is only temporary, you should use redirect instead of permanent.
131+
132+
## Redirects using mapping
133+
134+
The map directive allows you to define conditional logic outside of your main server block. This keeps your configuration:
135+
136+
- Easier to read
137+
- More scalable for large numbers of redirects
138+
- More efficient than stacking multiple if statements
139+
140+
You will define your redirects in two parts:
141+
142+
1. A mapping configuration (placed in an `/data/web/nginx/http.*` file)
143+
1. A rewrite rule (placed in a `/data/web/nginx/server.*` file)
144+
145+
Step 1: Define the Redirect Mapping
146+
147+
`/data/web/nginx/http.*`
148+
149+
```nginx
150+
map $uri $out_redirect {
151+
~/the-location-you-want-to-redirect /new-location;
152+
}
153+
```
154+
155+
You can expand this map with multiple rules:
156+
157+
```nginx
158+
map $uri $out_redirect {
159+
~/old-page /new-page;
160+
~/blog/(.*) /articles/$1;
161+
~/deprecated /;
162+
}
163+
```
164+
165+
Step 2: Apply the Redirect in the Server Block
166+
167+
`/data/web/nginx/server.*`
168+
169+
```nginx
170+
if ($out_redirect) {
171+
rewrite 301 $out_redirect;
172+
}
173+
```
174+
175+
Using `map` for redirects in NGINX provides a clean, scalable, and efficient way to manage URL rewrites. By separating logic from execution, your configuration remains easy to maintain even as the number of redirects grows.

0 commit comments

Comments
 (0)