# Traefik Rules for 2nd unsecured domain

**URL:** <https://community.traefik.io/t/traefik-rules-for-2nd-unsecured-domain/13697>\
**Category:** Traefik v2\
**Tags:** docker\
**Created:** [March 8, 2022, 3:22pm UTC](https://community.traefik.io/t/traefik-rules-for-2nd-unsecured-domain/13697 "2022-03-08T15:22:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mf1](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/mf1/32/5037_2.png) [@mf1](https://community.traefik.io/u/mf1)\
**Post date:** [March 8, 2022, 3:22pm UTC](https://community.traefik.io/t/traefik-rules-for-2nd-unsecured-domain/13697/1 "2022-03-08T15:22:40Z")

</div>

Hey guys.

I want to catch an old non-secured domain for a Redmine instance.

I have a Docker container with these labels:

```auto
labels:
        - "traefik.enable=true"
        - "traefik.http.routers.redmine.entrypoints=http"
        - "traefik.http.routers.redmine.rule=Host(`sub1.host.de`)"
        - "traefik.http.middlewares.redmine-https-redirect.redirectscheme.scheme=https"
        - "traefik.http.routers.redmine.middlewares=redmine-https-redirect"
        - "traefik.http.routers.redmine.rule=Host(`sub2.sub1.host.de`)" // 2nd domain
        - "traefik.http.routers.redmine.middlewares=redmine-regex2"
        - "traefik.http.middlewares.redmine2-regex.replacepathregex.regex=^sub2.sub1.host.de/(.*)"
        - "traefik.http.middlewares.redmine2-regex.replacepathregex.replacement=https://sub1.host.de/$$1"
        - "traefik.http.routers.redmine-secure.entrypoints=https"
        - "traefik.http.routers.redmine-secure.rule=Host(`sub.host.de`)"
        - "traefik.http.routers.redmine-secure.tls=true"
        - "traefik.http.routers.redmine-secure.tls.certresolver=http"
        - "traefik.http.routers.redmine-secure.service=redmine"
        - "traefik.http.services.redmine.loadbalancer.server.port=3000"
        - "traefik.docker.network=proxy"

```

How can I redirect the old domain [sub2.sub1.host.de](http://sub2.sub1.host.de) (http) to [sub1.host.de](http://sub1.host.de) (https)

---

<div class="post-metadata">

**Author:** ![tommoulard](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/tommoulard/32/2997_2.png) [@tommoulard](https://community.traefik.io/u/tommoulard)\
**Post date:** [March 8, 2022, 4:02pm UTC](https://community.traefik.io/t/traefik-rules-for-2nd-unsecured-domain/13697/2 "2022-03-08T16:02:14Z")

</div>

Hello @mf1,

You can use the [`RedirectRegex` middleware](https://doc.traefik.io/traefik/middlewares/http/redirectregex/) for this.

This can be something like this:

```yaml
labels:
  - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^http://sub2.sub1.host.de/(.*)"
  - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=https://sub1.host.de/$${1}"

```

---

<div class="post-metadata">

**Author:** ![mf1](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/mf1/32/5037_2.png) [@mf1](https://community.traefik.io/u/mf1)\
**Post date:** [March 8, 2022, 4:11pm UTC](https://community.traefik.io/t/traefik-rules-for-2nd-unsecured-domain/13697/3 "2022-03-08T16:11:31Z")

</div>

I have to remove those lines and add you two lined at the end?

```auto
        - "traefik.http.routers.redmine.rule=Host(`sub2.sub1.host.de`)" // 2nd domain
        - "traefik.http.routers.redmine.middlewares=redmine-regex2"
        - "traefik.http.middlewares.redmine2-regex.replacepathregex.regex=^sub2.sub1.host.de/(.*)"
        - "traefik.http.middlewares.redmine2-regex.replacepathregex.replacement=https://sub1.host.de/$$1"

```

I've tried this but get only `404 page not found`.

---

<div class="post-metadata">

**Author:** ![tommoulard](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/tommoulard/32/2997_2.png) [@tommoulard](https://community.traefik.io/u/tommoulard)\
**Post date:** [March 8, 2022, 4:21pm UTC](https://community.traefik.io/t/traefik-rules-for-2nd-unsecured-domain/13697/4 "2022-03-08T16:21:16Z")

</div>

Here's a simple example:

```yaml
version: '3.9'

services:
  traefik:
    image: traefik:v2.6
    command:
      - --providers.docker
      - --entrypoints.web.address=:80
      - --entrypoints.websecure.address=:443
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

  whoami:
    image: traefik/whoami
    labels:
      traefik.http.routers.whoami.rule: Host(`sub1.localhost`) || Host(`sub2.sub1.localhost`)
      traefik.http.routers.whoami.middlewares: test-redirectregex
      traefik.http.middlewares.test-redirectregex.redirectregex.regex: ^http://sub2.sub1.localhost/(.*)
      traefik.http.middlewares.test-redirectregex.redirectregex.replacement: https://sub1.localhost/$${1}

```

Which wields:

```auto
$ curl sub2.sub1.localhost -vvv
* Couldn't find host sub2.sub1.localhost in the (nil) file; using defaults
* Trying ::1:80...
* Connected to sub2.sub1.localhost (::1) port 80 (#0)
> GET / HTTP/1.1
> Host: sub2.sub1.localhost
> User-Agent: curl/7.79.0-DEV
> Accept: application/json, application/xml, text/plain, */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 302 Found
< Location: https://sub1.localhost/
< Date: Tue, 08 Mar 2022 16:19:14 GMT
< Content-Length: 5
< Content-Type: text/plain; charset=utf-8
< 
* Connection #0 to host sub2.sub1.localhost left intact
Found
$ curl sub1.localhost
Name: whoami
Hostname: 1236e601ba18
IP: 127.0.0.1
IP: 172.19.0.2
RemoteAddr: 172.19.0.3:55708
GET / HTTP/1.1
Host: sub1.localhost
User-Agent: curl/7.79.0-DEV
Accept: application/json, application/xml, text/plain, */*
Accept-Encoding: gzip
X-Forwarded-For: 172.19.0.1
X-Forwarded-Host: sub1.localhost
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Forwarded-Server: a7c4428ce953
X-Real-Ip: 172.19.0.1

```

---

<div class="post-metadata">

**Author:** ![mf1](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/mf1/32/5037_2.png) [@mf1](https://community.traefik.io/u/mf1)\
**Post date:** [March 8, 2022, 4:50pm UTC](https://community.traefik.io/t/traefik-rules-for-2nd-unsecured-domain/13697/5 "2022-03-08T16:50:04Z")

</div>

Got it... Thx.

Here is my modified setup ...

```auto
    labels:
        - "traefik.enable=true"
        - "traefik.http.routers.redmine.entrypoints=http"
        - "traefik.http.routers.redmine.rule=Host(`sub1.host.de`, `sub2.sub1.host.de`)"
        - "traefik.http.routers.redmine.middlewares=redmine-https-redirect"
        - "traefik.http.middlewares.redmine-https-redirect.redirectscheme.scheme=https"
        - "traefik.http.routers.redmine.middlewares=redmine-pub"
        - "traefik.http.middlewares.redmine-pub.redirectregex.regex=^http://sub2.sub1.host.de/(.*)"
        - "traefik.http.middlewares.redmine-pub.redirectregex.replacement=https://sub1.host.de/$${1}"
        - "traefik.http.routers.redmine-secure.entrypoints=https"
        - "traefik.http.routers.redmine-secure.rule=Host(`sub1.host.de`)"
        - "traefik.http.routers.redmine-secure.tls=true"
        - "traefik.http.routers.redmine-secure.tls.certresolver=http"
        - "traefik.http.routers.redmine-secure.service=redmine"
        - "traefik.http.services.redmine.loadbalancer.server.port=3000"
        - "traefik.docker.network=proxy"

```
