Rewrite URLs

Overview

You can use the BIG-IP Controller to rewrite URLs for Kubernetes Ingress resources and OpenShift Ingresses and Routes.

The Annotations are the same in both orchestration systems. The examples provided in this document are Kubernetes Ingress Resources. Their usage is no different in OpenShift Ingresses or Routes.

Important

If you use both of the virtual-server.f5.com rewrite Annotations for the same Service, the Controller applies them in the following order:

  1. app-root
  2. target-url

Usage

You can add the rewrite Annotations to any of the following resources:

  • Kubernetes Ingress
  • OpenShift Ingress
  • OpenShift Route

When using the Annotation(s) in a Single Service Ingress, Multi-Service Ingress, or Route resource, you can specify a single value in the annotation.

When using the Annotation(s) in a Multi-Service Ingress, you can specify a single value –OR– multiple, comma-separated values.

Examples

Rewrite App-Root

Use the rewrite-app-root Annotation to redirect requests for “/” to a different path.

Your Ingress or Route resource must have either of the following defined in the spec:

  • host without paths –OR–
  • host with redirect path
Redirect “/” to “/home” for all traffic
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    virtual-server.f5.com/rewrite-app-root: "/home"
  name: home
  namespace: default
spec:
  backend:
    serviceName: svc-home
    servicePort: 80
Rewrite “/” to “/en” for traffic with host “docs.example.com”
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    virtual-server.f5.com/rewrite-app-root: "docs.example.com=/en"
  name: docs-rewrite
  namespace: default
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /home
        backend:
          serviceName: svc-home
          servicePort: 80

  - host: docs.example.com
    http:
      paths:
      - path: /en
        backend:
          serviceName: svc-docs
          servicePort: 80
Rewrite multiple paths in a single annotation
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    virtual-server.f5.com/rewrite-app-root: "example.com=/home,docs.example.com=/en"
  name: photos-and-home
  namespace: default
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /home
        backend:
          serviceName: svc-home
          servicePort: 80

  - host: docs.example.com
    http:
      paths:
      - path: /en
        backend:
          serviceName: svc-docs
          servicePort: 80

Rewrite Target-URL

Use the rewrite-target-url Annotation to rewrite the exposed URL for a Service. It can rewrite the host, path, or both to the specified target.

Rewrite URL for all traffic to old.example.com
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    virtual-server.f5.com/rewrite-target-url: "old.example.com=new.example.com"
  name: rewrite-siteurl
  namespace: default
spec:
  rules:
  - host: old.example.com
    http:
      paths:
      - path: /my-site
        backend:
          serviceName: new-svc
          servicePort: 80
Rewrite from a specific host and path to a different host and path
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
   virtual-server.f5.com/rewrite-target-url: "old.example.com/contact=new.example.com/about"
  name: rewrite-about
  namespace: default
spec:
  rules:
  - host: old.example.com
    http:
      paths:
      - path: /contact
        backend:
          serviceName: old-svc
          servicePort: 80
- host: old.example.com
    http:
      paths:
      - path: /docs
        backend:
          serviceName: docs-svc
          servicePort: 80
Rewrite multiple paths in a single annotation
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    virtual-server.f5.com/rewrite-target-url: "old.example.com/contact=new.example.com/about,old.example.com/docs=new.example.com/docs"
  name: rewrite-about
  namespace: default
spec:
  rules:
  - host: old.example.com
    http:
      paths:
      - path: /contact
        backend:
          serviceName: old-svc
          servicePort: 80
- host: old.example.com
    http:
      paths:
      - path: /docs
        backend:
          serviceName: docs-svc
          servicePort: 80