How to make content to show on both www and non-www urls using traefik

admin

Administrator
Staff member
Thank you for showing interest and I was in a hurry. Any help would be great.
At present users were not able to reach www.example.com but they can reach example.com.

Either one of them is fine:

1) Accepts all traffic from WWW and non-www urls and serve same content.

2) Redirect users from WWW to non-www url to display content.

Note: Let's Encrypt is used

My present config is

<strong>traefik.toml</strong>

Code:
defaultEntryPoints = ["http", "https"]

[entryPoints]
  [entryPoints.dashboard]
    address = ":8080"
    [entryPoints.dashboard.auth]
      [entryPoints.dashboard.auth.basic]
        users = ["admin:key"]
  [entryPoints.http]
    address = ":80"
      [entryPoints.http.redirect]
      regex = "^https://www.(.*)"
      replacement = "https://$1"
      permanent=true
        entryPoint = "https"
  [entryPoints.https]
    address = ":443"
      [entryPoints.https.tls]

[api]
entrypoint="dashboard"

[acme]
email = "[email protected]"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
  [acme.httpChallenge]
  entryPoint = "http"
[[acme.domains]]
  main = "*.example.com"
  sans = ["example.com"]
[[acme.domains]]
  main = "*.example1.com"
  sans = ["example1.com"]
[docker]
domain = "example.com"
watch = true
network = "proxy"`

<strong>docker-compose.yml:</strong>

Code:
 version: '2'
services:
  traefik:
    image: traefik
    restart: always
    command: --docker
    ports:
      - 80:80
      - 443:443
    networks:
      - proxy
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - $PWD/traefik.toml:/traefik.toml
      - $PWD/acme.json:/acme.json
    container_name: trefik
    environment:
      DO_AUTH_TOKEN: TOKEN
    labels:
      - traefik.frontend.rule=Host:monitor.example.com
      - traefik.port=8080
  example1:
    image: wordpress:4.7.5-apache
    restart: always
    environment:
      WORDPRESS_DB_PASSWORD: something
    labels:
      - traefik.backend=example1
      - traefik.frontend.rule=Host:example1.com
      - traefik.docker.network=proxy
      - traefik.port=80
    networks:
      - internal
      - proxy
    depends_on:
      - mysql
  example:
   image: tutum/apache-php
   restart: always
   labels:
     - traefik.backend=example
     - traefik.frontend.rule=Host:example.com, www.example.com
     - traefik.docker.network=proxy
     - traefik.port=80
   networks:
     - internal
     - proxy

Edit #1:

Code:
Your config Redirects:

http://example.com =&gt; [no redirect]

https://www.example.com =&gt; [timeout]

http://www.example.com =&gt; [timeout]

http://example.com =&gt; [no redirect]

My Config Redirects:

http://example.com =&gt; https://example.com:443/

https://www.example.com =&gt; [timeout]

http://www.example.com =&gt; [timeout]

http://example.com =&gt; https://example.com:443/