URL Rewrite not working

Hi All,
I was trying to make URL rewriting thing workable but no luck yet. Appreciate your help. Below is the API definition. I followed the instructions mentioned in following document.

https://tyk.io/docs/transform-traffic/url-rewriting/

{

“name”: “Test API”,
“slug”: “user”,
“api_id”: “1”,
“org_id”: “1”,
“auth_configs”: {
“authToken”: {
“auth_header_name”: “Authorization”
}
},
“use_keyless”: true,
“definition”: {
“location”: “header”,
“key”: “x-api-version”
},
“version_data”: {
“not_versioned”: true,
“versions”: {
“Default”: {
“name”: “Default”,
“use_extended_paths”: true,
“extended_paths”: {
“url_rewrites”: [{
“path”: “match/me”,
“method”: “GET”,
“match_pattern”: “(\w+)/(\w+)”,
“rewrite_to”: “/user/get_services?user_id=$1”
}]
}
}
}
},
“proxy”: {
“listen_path”: “/match/”,
“target_url”: “http://x.x.x.x:4002”,
“strip_listen_path”: false
},
“active”: true
}

When I execute the URL end up with not found error.

Hi @Aruna_Attanayake

Could you share the result via the debugger in the dashboard. I have attached a screenshot below

If it works you should see an upstream and outbound request as shown below

The URL rewrite does not change your request URL. It only transforms it internally so that you can route it to the appropriate backend url/endpoint.

If you are having challenges, then please crosscheck your match pattern. Based on my API definition export, it should as follows

image

I hope this helps.

Thanks @Olu for the reply.

I tried with the same definition you have given here. But still not working.

What happen is when I type http://localhost:8080/user/234 in the browser URL, it makes a request to back end server in this format (/user/234)

What I expect the URL to be called to the back end is

user/get_services?user_id=234

I tried building out your example and the one thing I noticed that might be causing this issue is that your relative path is /user/{id}, which means that to reach this URL Rewrite endpoint you’d have to type in xxx.xx/user/user/234. When you’re entering in xxx.xx/user/234, you’re not completing the relative path so it’s following the standard /users/234 routing.

If my assessment here is correct, you’ll notice that http://localhost:8080/user/user/234 will trigger the URL Rewrite (though you may find the wrong parameter value is passed with the addition of another path component)

2 Likes

Thanks @BodhiMoosa . It worked.

1 Like