Hello Everybody,
I would like to redirect from non-www to www like Google does.
What I would like to do:
Type in the browser:
https://nomeadominio.it/
obtain:
https://www.nomeadominio.it/
Type in the browser:
https://www.nomeadominio.it/
obtain:
https://www.nomeadominio.it/
What I write:
version: "3.9"
services:
traefik:
...
command:
...
- --entrypoints.websecure.http.middlewares.redirect-non-www-to-www.redirectregex.permanent=true
- --entrypoints.websecure.http.middlewares.redirect-non-www-to-www.redirectregex.regex="^https?://(?:www\\.)?(.+)"
- --entrypoints.websecure.http.middlewares.redirect-non-www-to-www.redirectregex.replacement="https://www.${1}"
...
php:
...
labels:
traefik.enable: 'true'
traefik.http.routers.training1.rule: Host(`www.*********.ml`)
traefik.http.routers.training2.middlewares: redirect-non-www-to-www@file
...
Source from which I got the code: How to Redirect from Non-WWW to WWW with Traefik | by Benjamin Rancourt | Geek Culture | Medium
What I get:
ERROR: Invalid interpolation format for "command" option in service "traefik": "--entrypoints.websecure.http.middlewares.redirect-non-www-to-www.redirectregex.replacement="https://www.${1}""
ubuntu-22-04-lts@webserver:~/www.*********.tk$
What I think:
I cannot understand if it is a Traefik bug, if I am wrong to translate the code I find on the internet or if with the new versions of Traefik this operation is implemented differently. I do not find exhaustive official documentation regarding this functionality.
Other attempts I've made:
php:
...
labels:
traefik.enable: 'true'
traefik.http.routers.training1.rule: Host(`www.*****.ml`)
traefik.http.middlewares.redirect-non-www-to-www.redirectregex.permanent: true
traefik.http.middlewares.redirect-non-www-to-www.redirectregex.regex: "^https?://(?:www\\.)?(.+)"
traefik.http.middlewares.redirect-non-www-to-www.redirectregex.replacement: "https://www.$${1}"
traefik.http.routers.training2.middlewares: redirect-non-www-to-www@file
...
php:
...
labels:
traefik.enable: 'true'
traefik.http.routers.training.rule: Host(`www.*****.ml`)
traefik.http.middlewares.redirect-non-www-to-www.redirectregex.permanent: true
traefik.http.middlewares.redirect-non-www-to-www.redirectregex.regex: "^https?://(?:www\\.)?(.+)"
traefik.http.middlewares.redirect-non-www-to-www.redirectregex.replacement: "https://www.$${1}"
traefik.http.routers.training.middlewares: redirect-non-www-to-www@file
...
etc...
The above codes do not give compilation errors but do not work. As I wrote below I am looking for a global solution and not a specific one for each service.
What I don't want:
I don't want to have 2 valid URLs but only the one with www.
I don't want to write a specific implementation for each service but I want the redirect to be valid for all services and write the code only once.