How to rewrite url?

Hi :slight_smile:

I am using Tyk (v5.1.0) for a POC and I have difficulties using the rewrite_url functionality.

I would like that when sending a request to http://tky.gateway.localhost/test/cities Tyk validates the request body and forwards the request to http://nginx/service/test/v1/create_cities.
Unfortunately the request is rewritten to http://nginx/service/test/v1/cities.

I have defined the following OAS definition:

{
  "info": {
    "title": "cities webservice",
    "version": "1.0.0"
  },
  "openapi": "3.0.3",
  "security": [],
  "components": {
    "schemas": {
      "city": {
        "type": "object",
        "properties": {
          "name": {
            "description": "name of the city",
            "type": "string"
          },
          "country": {
            "description": "country name",
            "type": "string"
          }
        },
        "required": [
          "name",
          "country"
        ]
      }
    }
  },
  "paths": {
    "/cities": {
      "put": {
        "operationId": "addCities",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "cities": {
                    "type": "array",
                    "items" : {
                      "$ref": "#/components/schemas/city"
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": ""
          },
          "405":{
            "description":"Invalid input"
          }
        }
      }
    }
  },
  "x-tyk-api-gateway": {
    "info": {
      "name": "cities rewrite url",
      "state": {
        "active": true,
        "internal": false
      }
    },
    "middleware": {
      "operations": {
        "addCities": {
          "urlRewrite": {
            "enabled": true,
            "pattern": "/cities",
            "rewriteTo": "/create_cities"
          }
        }
      }
    },
    "server": {
      "listenPath": {
        "strip": true,
        "value": "/test/"
      }
    },
    "upstream": {
      "url": "http://nginx/service/test/v1"
    }
  }
}

I have also tried with different regex like:

  • .*/cities
  • /(\\w+)/cities
  • /test/cities

Can you please help to understand what I am doing wrong?

I have set the env-variable TYK_LOGLEVEL to debug so that I can have the debug log messages but I do not see any details how the matches conditions are mached or not.
Does a possibility exist to debug the url_rewrite ?
For eample if I send a request to the gateway I could have a debug message like:

urlRewrite is enabled for operaionId `addCities` 
check for urlRewrite rules...
urlRewrite pattern is set to /cities
url `http://tky.gateway.localhost/test/cities` do not match pattern `/cities` 
url rewrite from `http://tky.gateway.localhost/test/cities` to `http://nginx/service/test/v1/cities`

I have also tried with the example from the documentation and I do not see the url rewrited:

If I understand correctly the url should be transfromed:

But as you can see in the log below, the transformation didn’t ocurred.

time="Apr 08 10:37:41" level=debug msg="Started proxy"
time="Apr 08 10:37:41" level=debug msg="Stripping proxy listen path: /example-url-rewrite/"
time="Apr 08 10:37:41" level=debug msg="Upstream path is: /json/hello"
time="Apr 08 10:37:41" level=debug msg=Started api_id=0da74b182cdc4a4b87b473c7d3119010 api_name=example-url-rewrite mw=ReverseProxy org_id= ts=1712572661632807690
time="Apr 08 10:37:41" level=debug msg="Upstream request URL: /json/hello" api_id=0da74b182cdc4a4b87b473c7d3119010 api_name=example-url-rewrite mw=ReverseProxy org_id=
time="Apr 08 10:37:41" level=debug msg="Outbound request URL: http://httpbin.org/json/hello" api_id=0da74b182cdc4a4b87b473c7d3119010 api_name=example-url-rewrite mw=ReverseProxy org_id=
time="Apr 08 10:37:41" level=debug msg=Finished api_id=0da74b182cdc4a4b87b473c7d3119010 api_name=example-url-rewrite mw=ReverseProxy ns=277279933 org_id=
time="Apr 08 10:37:41" level=debug msg="Upstream request took (ms): 277.390233"
time="Apr 08 10:37:41" level=debug msg="Done proxy"

Thank you in advance for your help !

Resources I have used by implementing the rewrite_url:

In this post, a user has given a log output

Hi,
can you please remove the previous message? I got the error 403 and I am not able to edit the post anymore…
Thank you!

with the latest version I am not allowed to create the api anymore “Request APIID does not match that in Definition! For Update operations these must match.”

Would this be v5.3.0?

Is the 403 still present? I can observe the successfully edited your last post as quoted above

This is for Tyk classic API definitions. This isn’t related

I don’t think the URL rewrite feature for OAS was present in this version. Full middleware maturity for OAS was recently reached in v5.3.0. Trying v5.3.0 should result in a different output

1 Like

@Olu, thank you for your answer!

I have figured out that I was using the wrong Tyk version 2 times.
First, an old one v5.1.0 and then I use the docker tag docker.tyk.io/tyk-gateway/tyk-gateway:latest which does not point to the latest version v5.3 but to the 4.0…
Url rewriting works fine. I still do not have detailed debug information related to the matching rules.
Could you please provide an example output of such debug message with version 5.3?
Is there is a difference of debug log output whether I am created the API with OAS definition or classic?

The error 403 remains present, I am not able to edit the previous post…

I came accross version 5.1 because I took the wrong repo “tyk-gateway-docker”:

I can see the url rewriting rule with log level debug with version 5.3