# URL redirect with path

**URL:** <https://community.f5.com/t/url-redirect-with-path/69215>\
**Category:** Technical Forums\
**Tags:** irules, devops, redirect, application-delivery\
**Created:** [February 8, 2022, 3:38pm UTC](https://community.f5.com/t/url-redirect-with-path/69215 "2022-02-08T15:38:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Silver\_back](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@Silver\_back](https://community.f5.com/u/Silver_back)\
**Post date:** [February 8, 2022, 3:38pm UTC](https://community.f5.com/t/url-redirect-with-path/69215/1 "2022-02-08T15:38:41Z")

</div>

I am trying to create a redirect irule but is not a standard redirect. It basically needs to take the URL + path and redirect it to another website + the path of the old URL. E.g.

[www.abc.com/path1/subpath2/](http://www.abc.com/path1/subpath2/) → [https://www.def.com/path1/subpath1/](https://www.def.com/path1/subpath1/)  
[www.abc.com/path4/subpath5/](http://www.abc.com/path4/subpath5/) → [https://www.def.com/path4/subpath5/](https://www.def.com/path4/subpath5/)  
[www.abc.com/](http://www.abc.com/) - \> [https://www.def.com/](https://www.def.com/)

The Url is not always the same therefore it needs to be flexible. Basically apply everything after the “/” to the new URL.  
In a situation where there is no path it just execute a normal redirect. below is my attempt which isn’t working, not sure why.

when HTTP\_REQUEST {

if { ([string tolower [HTTP::host]] equals “[www.abc.com](http://www.abc.com)”) and ([HTTP::uri] starts\_with “/”) } {

HTTP::respond 301 Location “[[https://def.com](https://def.com)[HTTP::uri](https://def.com%5BHTTP::uri)]”

}else {

HTTP::respond 301 Location “[https://def.com/](https://def.com/)”

}

}

---

<div class="post-metadata">

**Author:** ![JRahm](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/jrahm/32/3327_2.png) [@JRahm](https://community.f5.com/u/JRahm)\
**Post date:** [February 8, 2022, 6:30pm UTC](https://community.f5.com/t/url-redirect-with-path/69215/2 "2022-02-08T18:30:21Z")

</div>

A couple comments/questions…

- All URLs start with “/”, so as long as the host is [www.abc.com,](http://www.abc.com,)&nbsp;you’ll never get to the else.
- There is no need to string tolower the host, that is case insensitive according to the RFC.
- Do you want any redirects if the host is not [www.abc.com?](http://www.abc.com?)&nbsp;If it’s not, there is no additional logic to handle that.

That said, assuming you don’t need any redirects for the non-matching host, here’s how I’d write that:

```tcl
when HTTP_REQUEST {
    if { ([HTTP::host] eq "www.abc.com") and not([HTTP::uri] eq "/") } {
        HTTP::respond 301 Location "http://jrahmtest.local[HTTP::uri]"
        event disable all
    } elseif { [HTTP::host] eq "www.abc.com" } {
        HTTP::respond 301 Location "http://jrahmtest.local/"
        event disable all
    }
}

```

Test results:

 ![test1.png](https://d20hrnpixdzcsd.cloudfront.net/original/3X/2/4/24b1a1ae69474fc27ec87a2a6594598e00e6ec3a.png)

 ![test2.png](https://d20hrnpixdzcsd.cloudfront.net/original/2X/8/8b8beb56745519dd0f6608a1f5a2525c22d265bf.png)

---

<div class="post-metadata">

**Author:** ![Silver\_back](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@Silver\_back](https://community.f5.com/u/Silver_back)\
**Post date:** [February 8, 2022, 7:02pm UTC](https://community.f5.com/t/url-redirect-with-path/69215/3 "2022-02-08T19:02:16Z")

</div>

jRahm thank you for your reply/suggestion. Unfortunately, it didn’t work properly.

[www.abc.com](http://www.abc.com)&nbsp;redirect to [www.def.com](http://www.def.com)&nbsp;work fine  
However,  
[www.abc.com](http://www.abc.com)/somepath/someotherpath/ redirect to [www.def.com/somepath/someotherpath/](http://www.def.com)&nbsp;did not work. It just redirects without the path.

---

<div class="post-metadata">

**Author:** ![JRahm](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/jrahm/32/3327_2.png) [@JRahm](https://community.f5.com/u/JRahm)\
**Post date:** [February 8, 2022, 7:56pm UTC](https://community.f5.com/t/url-redirect-with-path/69215/4 "2022-02-08T19:56:45Z")

</div>

do you have a dump capturing what the BIG-IP is returning? you could also capture the redirect in HTTP\_RESPONSE\_RELEASE I think. Its working on my end, not sure what the issue is, will need to see what you’re seeing.

---

<div class="post-metadata">

**Author:** ![Silver\_back](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@Silver\_back](https://community.f5.com/u/Silver_back)\
**Post date:** [February 11, 2022, 8:14am UTC](https://community.f5.com/t/url-redirect-with-path/69215/5 "2022-02-11T08:14:30Z")

</div>

We were unable to get the iRule to work, therefore we sent the traffic to an apache server and on the server, we were able to configure the redirect to the right path. Thank you for your help.

---

<div class="post-metadata">

**Author:** ![JRahm](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/jrahm/32/3327_2.png) [@JRahm](https://community.f5.com/u/JRahm)\
**Post date:** [February 11, 2022, 1:48pm UTC](https://community.f5.com/t/url-redirect-with-path/69215/6 "2022-02-11T13:48:43Z")

</div>

Glad you found a solution! If you want to keep working it happy to help, this is totally doable with an iRule, we must be missing a piece of logic.
