Community Plugin: This plugin is developed, tested, and maintained by a third-party contributor.
When using Kong, you can create routes that proxy to an upstream. The problem lies when the upstream has an url that is not very friendly to your clients, or restful, or even pretty. When you add a Route in Kong, you have a somewhat limited url rewrite capability. This plugin simply throws away the url set in Kong route and uses the url set in it’s configuration to proxy to the upstream. This gives you full freedom as to how to write your url’s in Kong and inner services as well.
Kong Plugin URL Rewrite
For questions, details or contributions, please reach us here.
Configuration Reference
This plugin is not compatible with DB-less mode.
Enable the plugin on a service
Admin API
Kubernetes
Declarative (YAML)
For example, configure this plugin on a service by
making the following request:
curl -X POST http://{HOST}:8001/services/{SERVICE}/plugins \
--data "name=kong-plugin-url-rewrite" \
--data "config.url=http://new-url.com"
SERVICE
is the id
or name
of the service that this plugin
configuration will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: <kong-plugin-url-rewrite-example>
config:
url: http://new-url.com
plugin: kong-plugin-url-rewrite
Next, apply the KongPlugin resource to a
Service by annotating the
Service as follows:
apiVersion: v1
kind: Service
metadata:
name: {SERVICE}
labels:
app: {SERVICE}
annotations:
konghq.com/plugins: <kong-plugin-url-rewrite-example>
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: {SERVICE}
selector:
app: {SERVICE}
{SERVICE}
is the id
or name
of the service that this plugin
configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
For example, configure this plugin on a service by
adding this section to your declarative configuration file:
plugins:
- name: kong-plugin-url-rewrite
service: {SERVICE}
config:
url: http://new-url.com
SERVICE
is the id
or name
of the service that this plugin
configuration will target.
Enable the plugin on a route
Admin API
Kubernetes
Declarative (YAML)
For example, configure this plugin on a route with:
$ curl -X POST http://{HOST}:8001/routes/{ROUTE}/plugins \
--data "name=kong-plugin-url-rewrite" \
--data "config.url=http://new-url.com"
ROUTE
is the id
or name
of the route that this plugin configuration
will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: <kong-plugin-url-rewrite-example>
config:
url: http://new-url.com
plugin: kong-plugin-url-rewrite
Then, apply it to an ingress (Route or Routes)
by annotating the ingress as follows:
apiVersion: networking/v1beta1
kind: Ingress
metadata:
name: {ROUTE}
annotations:
kubernetes.io/ingress.class: kong
konghq.com/plugins: <kong-plugin-url-rewrite-example>
spec:
rules:
- host: examplehostname.com
http:
paths:
- path: /bar
backend:
serviceName: echo
servicePort: 80
ROUTE
is the id
or name
of the route that this plugin configuration
will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
For example, configure this plugin on a route by
adding this section to your declarative configuration file:
plugins:
- name: kong-plugin-url-rewrite
route: <route>
config:
url: http://new-url.com
ROUTE
is the id
or name
of the route that this plugin configuration
will target.
Enable the plugin globally
A plugin which is not associated to any service, route, or consumer is
considered global, and will be run on every request. Read the
Plugin Reference and the Plugin Precedence
sections for more information.
Admin API
Kubernetes
Declarative (YAML)
For example, configure this plugin globally with:
$ curl -X POST http://{HOST}:8001/plugins/ \
--data "name=kong-plugin-url-rewrite" \
--data "config.url=http://new-url.com"
Create a KongClusterPlugin
resource and label it as global:
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
name: <global-kong-plugin-url-rewrite>
annotations:
kubernetes.io/ingress.class: kong
labels:
global: \"true\"
config:
url: http://new-url.com
plugin: kong-plugin-url-rewrite
For example, configure this plugin using the plugins:
entry in the declarative
configuration file:
plugins:
- name: kong-plugin-url-rewrite
config:
url: http://new-url.com
Parameters
Here's a list of all the parameters which can be used in this plugin's configuration:
Form Parameter |
Description |
name
required
Type: string |
The name of the plugin, in this case kong-plugin-url-rewrite . |
service.id
Type: string |
The ID of the Service the plugin targets. |
route.id
Type: string |
The ID of the Route the plugin targets. |
enabled
required
Type: boolean
Default value: true |
Whether this plugin will be applied. |
api_id
Type: string |
The ID of the API the plugin targets.
Note: The API Entity is deprecated in favor of Services since CE 0.13.0 and EE 0.32. |
config.url
Default value:
|
The url that you want to execute the request against. Completely overrides the upstream_uri property.
|