Skip to content

Commit 585c2af

Browse files
committed
Traefik: introduce domain redirect
1 parent 6f90f13 commit 585c2af

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

services/traefik/docker-compose.yml.j2

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,28 @@ services:
146146
- traefik.http.middlewares.strip-www.redirectregex.replacement=$${1}://$${2}
147147
- traefik.http.middlewares.strip-www.redirectregex.permanent=true
148148
149+
150+
###
151+
# Domain redirects
152+
###
153+
{% set redirect_from_domains_list = TRAEFIK_DOMAINS_REDIRECT_FROM.split(',') %}
154+
{% set redirect_to_domains_list = TRAEFIK_DOMAINS_REDIRECT_TO.split(',') %}
155+
156+
{% for from_domain, to_domain in zip(redirect_from_domains_list, redirect_to_domains_list) %}
157+
{% set from_domain_no_dots = from_domain.replace(".", "-") %}
158+
# Regex below is redirecting any subdomains and path to new domain.
159+
# Use https://regex101.com/r/58sIgx/2 for regex explanation and experimentation.
160+
# Below we include dollar escaping and j2 expressions. It is not clean / pure regex
161+
# You can fetch baked and clean regex from traefik dashboards.
162+
- traefik.http.middlewares.redirect-{{ from_domain_no_dots }}.redirectregex.regex=^https?://((?:[a-zA-Z0-9-]+\.)*)*{{ from_domain }}(.*)$$
163+
- traefik.http.middlewares.redirect-{{ from_domain_no_dots }}.redirectregex.replacement=https://$${1}{{ to_domain }}$${2}
164+
- traefik.http.middlewares.redirect-{{ from_domain_no_dots }}.redirectregex.permanent=false
165+
- traefik.http.routers.{{ from_domain_no_dots }}.rule={{ generate_domain_capture_all_rule(from_domain) }}
166+
- traefik.http.routers.{{ from_domain_no_dots }}.middlewares=redirect-{{ from_domain_no_dots }}
167+
- traefik.http.routers.{{ from_domain_no_dots }}.entrypoints=https
168+
- traefik.http.routers.{{ from_domain_no_dots }}.tls=true
169+
{% endfor %}
170+
149171
networks:
150172
public: null
151173
monitored: null
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def _generate_domain_capture_all_rule(domain: str) -> str:
2+
rules = [
3+
f"Host(`{domain}`)",
4+
f"Host(`www.{domain}`)",
5+
f"Host(`invitations.{domain}`)",
6+
f"Host(`services.{domain}`)",
7+
f"HostRegexp(`{{subhost:[a-zA-Z0-9-]+}}.services.{domain}`)",
8+
f"Host(`services.testing.{domain}`)",
9+
f"HostRegexp(`{{subhost:[a-zA-Z0-9-]+}}.services.testing.{domain}`)",
10+
f"Host(`pay.{domain}`)",
11+
f"Host(`api.{domain}`)",
12+
f"Host(`api.testing.{domain}`)",
13+
f"Host(`testing.{domain}`)",
14+
]
15+
return " || ".join(rules)
16+
17+
18+
def j2_environment(env):
19+
env.globals.update(
20+
generate_domain_capture_all_rule=_generate_domain_capture_all_rule,
21+
zip=zip,
22+
)
23+
return env

services/traefik/template.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ MONITORED_NETWORK=${MONITORED_NETWORK}
3737

3838
WEBSERVER_HOST=${WEBSERVER_HOST}
3939
WEBSERVER_PORT=${WEBSERVER_PORT}
40+
41+
TRAEFIK_DOMAINS_REDIRECT_FROM=
42+
TRAEFIK_DOMAINS_REDIRECT_TO=

0 commit comments

Comments
 (0)