# adding to a redirect

**URL:** <https://community.f5.com/t/adding-to-a-redirect/33562>\
**Category:** Technical Forums\
**Tags:** irules, dev, devops, application-delivery\
**Created:** [August 3, 2010, 6:25pm UTC](https://community.f5.com/t/adding-to-a-redirect/33562 "2010-08-03T18:25:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albert\_Tase\_70](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Albert\_Tase\_70](https://community.f5.com/u/Albert_Tase_70)\
**Post date:** [August 3, 2010, 6:25pm UTC](https://community.f5.com/t/adding-to-a-redirect/33562/1 "2010-08-03T18:25:46Z")

</div>

Hello need some help trying to grab a piece of the uri and append it to the redirect

for example user goes to [www.bbb.com](http://www.bbb.com) wants to get redirect via a 301 to [https://bbb.com/yyyy](https://bbb.com/yyyy) and if the users adds

[www.bbb.com/nnn](http://www.bbb.com/nnn) they want to append that to the [https://bbb.com/yyy/nnn](https://bbb.com/yyy/nnn) got the basic syntax just cannot seem to figure out how to add the uri to the redirects uri for anything after the /yyy.

any Ideas ?

thanks

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [August 3, 2010, 8:24pm UTC](https://community.f5.com/t/adding-to-a-redirect/33562/2 "2010-08-03T20:24:38Z")

</div>

Hi Al,

Are you wanting to parse the portion of the original request from the query string (like a parameter name and/or value) or part of the path?

For an example URL, here are the different portions of the request:

[http://www.example.com/path/to/file.ext?param1=value1&param2=value2](http://www.example.com/path/to/file.ext?param1=value1&param2=value2)

Host - [HTTP::host] = [www.example.com](http://www.example.com)

Path - [HTTP::path] = /path/to/file.ext

URI - [HTTP::uri] = /path/to/file.ext?param1=value1&param2=value2

Query string = [HTTP::query] = param1=value1&param2=value2

Query string parameter value = value1 and value2, use URI::query workaround to get the values (in pre-10.2 versions):

[http://devcentral.f5.com/wiki/default.aspx/iRules/uri\_\_query](http://devcentral.f5.com/wiki/default.aspx/iRules/uri%5C_%5C_query)

[URI::query "?&[HTTP::query] &param\_name

To further parse the path, you can use URI::path:

[http://devcentral.f5.com/wiki/default.aspx/iRules/uri\_\_path](http://devcentral.f5.com/wiki/default.aspx/iRules/uri%5C_%5C_path)

Aaron

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [August 6, 2010, 11:52am UTC](https://community.f5.com/t/adding-to-a-redirect/33562/3 "2010-08-06T11:52:27Z")

</div>

I don’t think user-agents will include anchors in the request sent to the web app. The browser should make a request using the URI minus the anchor and then use the anchor to go to a specific section in the page. You can test this by logging the HTTP::uri value for a request like [https://www.example.com/xyx/trackingid=8888](https://www.example.com/xyx/trackingid=8888) and see if the URI is logged as / or /xyx/trackingid=8888.

In concept though, you can send a 301 redirect using:

HTTP::respond 301 Location “[https://www.example.com/new\_uri](https://www.example.com/new%5C_uri)”

Aaron

---

<div class="post-metadata">

**Author:** ![Albert\_Tase\_70](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Albert\_Tase\_70](https://community.f5.com/u/Albert_Tase_70)\
**Post date:** [August 6, 2010, 1:25pm UTC](https://community.f5.com/t/adding-to-a-redirect/33562/4 "2010-08-06T13:25:58Z")

</div>

ok but how ?

I would have to query the uri comming in and make it a varable quessing here

but then how to add it to the HTTP::respond 301 Location “[https://www.example.com/new\_uri](https://www.example.com/new%5C_uri)”

because I need to redirect it to [https://www.example.com/xyx](https://www.example.com/xyx) no matter what and then add what ever is typed after

the [www.example.com](http://www.example.com) to https:/www.example.com/xyx so if the users types [www.example.com](http://www.example.com) they get redirected via a 301 to [https://www.example2.com/xyx](https://www.example2.com/xyx) if the users types [www.example.com/trackingid=8888](http://www.example.com/trackingid=8888) they need to get redirected to [https://www.eample2.com/xyx/trackingid=8888](https://www.eample2.com/xyx/trackingid=8888) this is so the app server can grep the id what ever it be and log it.

so it would be an if else statement if http::host equals [www.example.com](http://www.example.com)

http::respond 301 location "[https://www.eaxmple2.com/xyx](https://www.eaxmple2.com/xyx)

else

http::response 301 location "[https://www.example2.com/xyx/](https://www.example2.com/xyx/) whatever comes after the [www.example.com](http://www.example.com)

the issue is that the /xyx is a;ready part of ther uri that I need to redirect to which I have to set on the lb then I need to add to it from whatever the user types as the uri after the [www.example.com](http://www.example.com)

basically not sure how to do this if it can be done

so if user a types [www.example.com](http://www.example.com) he needs to get redirected via 301

to

if user a types [www.example.com/trackingid=8888](http://www.example.com/trackingid=8888) he needs to get redirected

to [2.com/xyx/trackingid=8888](http://2.com/xyx/trackingid=8888)

in the second part the user can typ anying after the host the uri the user types needs to get preserved ansd added to

the redirected uri /xyx/

---

<div class="post-metadata">

**Author:** ![Colin\_Walker\_12](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@Colin\_Walker\_12](https://community.f5.com/u/Colin_Walker_12)\
**Post date:** [August 11, 2010, 10:37pm UTC](https://community.f5.com/t/adding-to-a-redirect/33562/5 "2010-08-11T22:37:52Z")

</div>

It sounds like all you’re trying to do is redirect to a second host name, preserving the URI, but making sure that no matter what the URI was, it starts with /xyz/ first. Your concern is that you don’t want to end up with a URI that starts with /xyz/xyz/, right? A simple if clause will solve that problem for you.

So I think what you’re looking for is something like:

```tcl
when HTTP_REQUEST {
  if {[HTTP::uri] starts_with "/xyz" } {
    HTTP::redirect "http://example2.com[HTTP::uri]"
  } else {
    HTTP::redirect "http://example2.com/xyz[HTTP::uri]"
  }
}

```

Does that sound about right? If I missed something, let me know.

Colin
