Skip to content

Commit 0e3b60d

Browse files
committed
Added global redirect middlewares
1 parent 05ef073 commit 0e3b60d

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

configs-local/dynamic/global-middlewares.yml

+26
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,29 @@ http:
2222
stsIncludeSubdomains: true
2323
stsPreload: true
2424
forceSTSHeader: true
25+
26+
# Redirect non-www URLs to their www equivalent
27+
# Use with traefik.http.routers.myRouter.middlewares: "redirect-non-www-to-www@file"
28+
# Source: https://www.benjaminrancourt.ca/how-to-redirect-from-non-www-to-www-with-traefik/
29+
redirect-non-www-to-www:
30+
# Redirect a request from an url to another with regex matching and replacement
31+
redirectregex:
32+
# Apply a permanent redirection (HTTP 301)
33+
permanent: true
34+
# The regular expression to match and capture elements from the request URL
35+
regex: "^https?://(?:www\\.)?(.+)"
36+
# How to modify the URL to have the new target URL
37+
replacement: "https://www.${1}"
38+
39+
# Redirect www URLs to their non-www equivalent
40+
# Use with traefik.http.routers.myRouter.middlewares: "redirect-www-to-non-www@file"
41+
# Source: https://www.benjaminrancourt.ca/how-to-redirect-from-non-www-to-www-with-traefik/
42+
redirect-www-to-non-www:
43+
# Redirect a request from an url to another with regex matching and replacement
44+
redirectregex:
45+
# Apply a permanent redirection (HTTP 301)
46+
permanent: true
47+
# The regular expression to match and capture elements from the request URL
48+
regex: "^https?://www\\.(.+)"
49+
# How to modify the URL to have the new target URL
50+
replacement: "https://${1}"

configs-prod/dynamic/global-middlewares.yml

+26
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,29 @@ http:
2222
stsIncludeSubdomains: true
2323
stsPreload: true
2424
forceSTSHeader: true
25+
26+
# Redirect non-www URLs to their www equivalent
27+
# Use with traefik.http.routers.myRouter.middlewares: "redirect-non-www-to-www@file"
28+
# Source: https://www.benjaminrancourt.ca/how-to-redirect-from-non-www-to-www-with-traefik/
29+
redirect-non-www-to-www:
30+
# Redirect a request from an url to another with regex matching and replacement
31+
redirectregex:
32+
# Apply a permanent redirection (HTTP 301)
33+
permanent: true
34+
# The regular expression to match and capture elements from the request URL
35+
regex: "^https?://(?:www\\.)?(.+)"
36+
# How to modify the URL to have the new target URL
37+
replacement: "https://www.${1}"
38+
39+
# Redirect www URLs to their non-www equivalent
40+
# Use with traefik.http.routers.myRouter.middlewares: "redirect-www-to-non-www@file"
41+
# Source: https://www.benjaminrancourt.ca/how-to-redirect-from-non-www-to-www-with-traefik/
42+
redirect-www-to-non-www:
43+
# Redirect a request from an url to another with regex matching and replacement
44+
redirectregex:
45+
# Apply a permanent redirection (HTTP 301)
46+
permanent: true
47+
# The regular expression to match and capture elements from the request URL
48+
regex: "^https?://www\\.(.+)"
49+
# How to modify the URL to have the new target URL
50+
replacement: "https://${1}"

readme.md

+14
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@ The `max-age` is set to one year / 31536000 seconds.
178178

179179
Adds a permanent redirect to HTTPS.
180180

181+
**redirect-non-www-to-www@file**
182+
183+
Adds a permanent redirect (HTTP 301) from non-www domains to the HTTPS www domain
184+
Examples:
185+
- `https://example.test` -> `https://www.example.test`
186+
- `http://example.test` -> `https://www.example.test`
187+
188+
**redirect-www-to-non-www@file**
189+
190+
Adds a permanent redirect (HTTP 301) from www domains to the HTTPS non-www domain
191+
Examples:
192+
- `https://www.example.test` -> `https://example.test`
193+
- `http://www.example.test` -> `https://example.test`
194+
181195
### Access Logs
182196

183197
To enable the traefik access logs in the production configuration, open the file `traefik.yml` within the config folder and uncomment the `accessLog` section.

0 commit comments

Comments
 (0)