@@ -146,6 +146,35 @@ 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+ {% set redirect_is_permanent_list = TRAEFIK_DOMAINS_REDIRECT_IS_PERMANENT.split(' ,') %}
156+
157+ {% for ix in range(redirect_from_domains_list | length) %}
158+
159+ {% set from_domain = redirect_from_domains_list[ix] %}
160+ {% set from_domain_no_dots = from_domain.replace(".", "-") %}
161+ {% set to_domain = redirect_to_domains_list[ix] %}
162+ {% set redirect_is_permanent = redirect_is_permanent_list[ix] %}
163+
164+ # Regex below is redirecting any subdomains and path to new domain.
165+ # Use https://regex101.com/r/58sIgx/2 for regex explanation and experimentation.
166+ # Below we include dollar escaping and j2 expressions. It is not clean / pure regex
167+ # You can fetch baked and clean regex from traefik dashboards.
168+ - traefik.http.middlewares.redirect-{{ from_domain_no_dots }}.redirectregex.regex=^https?://((?:[a-zA-Z0-9-]+\.)*)*{{ from_domain }}(.*)$$
169+ - traefik.http.middlewares.redirect-{{ from_domain_no_dots }}.redirectregex.replacement=https://$${1}{{ to_domain }}$${2}
170+ - traefik.http.middlewares.redirect-{{ from_domain_no_dots }}.redirectregex.permanent={{ redirect_is_permanent }}
171+ - traefik.http.routers.{{ from_domain_no_dots }}.rule={{ generate_domain_capture_all_rule(from_domain) }}
172+ - traefik.http.routers.{{ from_domain_no_dots }}.middlewares=redirect-{{ from_domain_no_dots }}
173+ - traefik.http.routers.{{ from_domain_no_dots }}.entrypoints=https
174+ - traefik.http.routers.{{ from_domain_no_dots }}.tls=true
175+
176+ {% endfor %}
177+
149178 networks:
150179 public: null
151180 monitored: null
0 commit comments