Skip to content

Redirect HTTP to HTTPS


Configure redirection

---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: httpbin-redirect-to-https
spec:
  hostnames:
  - httpbin.esuez.org
  parentRefs:
  - name: eg
    sectionName: http
  rules:
  - filters:
    - type: RequestRedirect
      requestRedirect:
        scheme: https
        statusCode: 301
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: httpbin
spec:
  hostnames:
  - httpbin.esuez.org
  parentRefs:
  - name: eg
    sectionName: https
  rules:
  - backendRefs:
    - name: httpbin
      port: 8000
kubectl apply -f redirect/httpbin-to-https.yaml

✅ Test

Verify that you get a 301 response when curling the http endpoint:

curl -v http://httpbin.esuez.org/
curl -v http://httpbin.esuez.org/ --resolve httpbin.esuez.org:80:$GATEWAY_IP

Here is a copy of the captured output:

* Host httpbin.esuez.org:80 was resolved.
* IPv6: (none)
* IPv4: 34.121.222.176
*   Trying 34.121.222.176:80...
* Connected to httpbin.esuez.org (34.121.222.176) port 80
> GET / HTTP/1.1
> Host: httpbin.esuez.org
> User-Agent: curl/8.7.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 301 Moved Permanently
< location: https://httpbin.esuez.org:443/
< date: Tue, 07 May 2024 21:21:19 GMT
< content-length: 0
<
* Connection #0 to host httpbin.esuez.org left intact