# How to make HAProxy's SSL redirect and path rewrite (with reqrep) work at the same time?

**URL:** <https://discourse.haproxy.org/t/how-to-make-haproxys-ssl-redirect-and-path-rewrite-with-reqrep-work-at-the-same-time/2677>\
**Category:** Help!\
**Created:** [June 30, 2018, 3:07pm UTC](https://discourse.haproxy.org/t/how-to-make-haproxys-ssl-redirect-and-path-rewrite-with-reqrep-work-at-the-same-time/2677 "2018-06-30T15:07:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xji](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/xji/32/536_2.png) [@xji](https://discourse.haproxy.org/u/xji)\
**Post date:** [June 30, 2018, 3:07pm UTC](https://discourse.haproxy.org/t/how-to-make-haproxys-ssl-redirect-and-path-rewrite-with-reqrep-work-at-the-same-time/2677/1 "2018-06-30T15:07:56Z")

</div>

I need to deploy different apps on the same domain, thus I’ve set up the `backend` to rewrite URL with `reqrep ^([^\]*\ /)appA[/]?(.*) \1\2`. It works when I only deploy the HTTP or HTTPS version of the app.

However, if I try to redirect all HTTP traffic to HTTPS, it doesn’t work. The problem is that HAProxy has already rewritten the URL and stripped out the `/appA` part before the redirection. Thus if I try to visit `http://myserver.com/appA`, the page `https://myserver.com` will eventually be requested instead of `https://myserver.com/appA`.

I can’t put the redirect rule before the reqrep rule as it seems that HAProxy must process all the rewrites before the redirects.

What can I do to make my configuration work as I intended? This should be obvious but I can’t seem to find a relevant answer online.

My configuration:

```
frontend http_front
  bind *:80
  reqadd X-Forwarded-Proto:\ http
  acl appA_url url_beg /appA
  use_backend appA if appA_url

frontend https_front
  bind *:443 ssl crt /etc/haproxy/certs/myserver.com.pem
  reqadd X-Forwarded-Proto:\ https
  acl appA_url url_beg /appA
  use_backend appA if appA_url

backend appA
  reqrep ^([^\]*\ /)appA[/]?(.*) \1\2
  redirect scheme https if !{ ssl_fc }
  balance roundrobin
  server web1 127.0.0.1:5001 check
```

---

<div class="post-metadata">

**Author:** ![arash](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/arash/32/255_2.png) [@arash](https://discourse.haproxy.org/u/arash)\
**Post date:** [July 2, 2018, 12:55am UTC](https://discourse.haproxy.org/t/how-to-make-haproxys-ssl-redirect-and-path-rewrite-with-reqrep-work-at-the-same-time/2677/2 "2018-07-02T00:55:59Z")

</div>

If I understand correctly this is your scenario

1- User sends a request to [http://site.com/appA](http://site.com/appA)  
2- haproxy redirect http to https  
3- haproxy choose backend  
4- haproxy needs to remove /appA from path and sends the request to backend .

```
frontend http-in
    bind 0.0.0.0:80
    http-request redirect scheme https
    default_backend maintenance

frontend https-in
    bind 0.0.0.0:443 ssl crt /etc/haproxy/certs alpn h2,http/1.1
    http-request add-header X-Forwarded-Proto https
    acl appA_url url_beg /appA
    use_backend appA if appA_url
    default_backend maintenance

backend appA
    balance roundrobin
    http-request set-uri %[url,regsub(^/appA,/,)] if { path_beg /appA }
    server web1 127.0.0.1:5001 check

backend maintenance
    balance roundrobin
```

---

<div class="post-metadata">

**Author:** ![xji](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/xji/32/536_2.png) [@xji](https://discourse.haproxy.org/u/xji)\
**Post date:** [July 9, 2018, 12:59pm UTC](https://discourse.haproxy.org/t/how-to-make-haproxys-ssl-redirect-and-path-rewrite-with-reqrep-work-at-the-same-time/2677/3 "2018-07-09T12:59:07Z")

</div>

Thanks for the solution. Another one I got on Stackoverflow is:

```auto
http-request redirect scheme https if ! { ssl_fc }
http-request set-path %[path,regsub(^/appA/?,/)]

```

in the backend.

The key seems to be to use `http-request` instead of the older `reqrep`.

---

<div class="post-metadata">

**Author:** ![kanwar](https://avatars.discourse-cdn.com/v4/letter/k/5f8ce5/32.png) [@kanwar](https://discourse.haproxy.org/u/kanwar)\
**Post date:** [July 7, 2020, 8:26am UTC](https://discourse.haproxy.org/t/how-to-make-haproxys-ssl-redirect-and-path-rewrite-with-reqrep-work-at-the-same-time/2677/4 "2020-07-07T08:26:55Z")

</div>

Is it possible to just change the case of a URL partially, say just one section of the URL must be converted to uppercase?
