# Root Domain Redirect Retaining Query String

**URL:** <https://community.f5.com/t/root-domain-redirect-retaining-query-string/29321>\
**Category:** Technical Forums\
**Tags:** irules, dev, devops, application-delivery\
**Created:** [November 13, 2010, 3:22pm UTC](https://community.f5.com/t/root-domain-redirect-retaining-query-string/29321 "2010-11-13T15:22:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scott\_85950](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Scott\_85950](https://community.f5.com/u/Scott_85950)\
**Post date:** [November 13, 2010, 3:22pm UTC](https://community.f5.com/t/root-domain-redirect-retaining-query-string/29321/1 "2010-11-13T15:22:43Z")

</div>

~~Hi, I am looking to redirect, hopefully 301 redirect, from the default domain when called to a new home page location while retaining any appended query in the original URI. I have the following iRule created, which does work, but the query is not retained when redirected. Please help! .~~

~~when HTTP\_REQUEST {~~

~~set my\_uri [HTTP::uri]~~

~~set my\_query [HTTP::query]~~

~~if { ([HTTP::host] eq “[mysite.com](http://mysite.com)”) } {~~

~~switch -glob [HTTP::uri] {~~

~~“/” { HTTP::redirect “http://[HTTP::host]/guest/en/home.shtml$my\_query” }~~

~~}~~

~~}~~

~~}~~

---

<div class="post-metadata">

**Author:** ![Scott\_85950](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Scott\_85950](https://community.f5.com/u/Scott_85950)\
**Post date:** [November 13, 2010, 3:25pm UTC](https://community.f5.com/t/root-domain-redirect-retaining-query-string/29321/2 "2010-11-13T15:25:39Z")

</div>

Expected behavior:

[Website Hosting - Mysite.com](http://www.mysite.com/?s)\_cid=ThisIsATest --\> [www.mysite.com/guest/en/home.shtml?s](http://www.mysite.com/guest/en/home.shtml?s)\_cid=ThisIsATest

---

<div class="post-metadata">

**Author:** ![nitass](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/nitass/32/2270_2.png) [@nitass](https://community.f5.com/u/nitass)\
**Post date:** [November 13, 2010, 3:50pm UTC](https://community.f5.com/t/root-domain-redirect-retaining-query-string/29321/3 "2010-11-13T15:50:41Z")

</div>

what about this one?

```tcl
when HTTP_REQUEST {
    if { [HTTP::path] equals "/" } {
              HTTP::redirect "http://[HTTP::host]/guest/en/home.shtml?[HTTP::query]" }
    }
 }

```

---

<div class="post-metadata">

**Author:** ![nitass](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/nitass/32/2270_2.png) [@nitass](https://community.f5.com/u/nitass)\
**Post date:** [November 13, 2010, 4:12pm UTC](https://community.f5.com/t/root-domain-redirect-retaining-query-string/29321/4 "2010-11-13T16:12:06Z")

</div>

i a little bit modified rule according to sol9952. pls feel free to revise.

SOL9952: The HTTP::path iRule command may return more information than expected

> **[404 Page not found | BIG-IP Documentation](https://techdocs.f5.com/kb/en-us/solutions/public/9000/900/sol9952.html)**
>
> Usage information and technical documentation for BIG-IP and other related F5 products

```tcl
virtual bar {
   snat automap
   pool foo
   destination 172.28.17.55:80
   ip protocol 6
   rules myredirect
   profiles {
      http {}
      tcp {}
   }
}
rule myredirect {
   when HTTP_REQUEST {
        if { [URI::path [HTTP::path]] eq "/" and [HTTP::query] ne ""} {
                HTTP::redirect "http://[HTTP::host]/guest/en/home.shtml?[HTTP::query]"
        }
}
}

```

```tcl
curl -I http://172.28.17.55
HTTP/1.1 200 OK
Date: Sat, 13 Nov 2010 07:28:33 GMT
Server: Apache/2.0.59 (rPath)
Last-Modified: Thu, 11 Nov 2010 04:23:21 GMT
ETag: "1315d-5f-5580a040"
Accept-Ranges: bytes
Content-Length: 95
Content-Type: text/html; charset=UTF-8

 curl -I http://172.28.17.55/?s_cid=ThisIsATest
HTTP/1.0 302 Found
Location: http://172.28.17.55/guest/en/home.shtml?s_cid=ThisIsATest
Server: BigIP
Connection: Keep-Alive
Content-Length: 0

```

---

<div class="post-metadata">

**Author:** ![Scott\_85950](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Scott\_85950](https://community.f5.com/u/Scott_85950)\
**Post date:** [November 13, 2010, 8:03pm UTC](https://community.f5.com/t/root-domain-redirect-retaining-query-string/29321/5 "2010-11-13T20:03:49Z")

</div>

Thank you, this works great. I am going to slightly modify with the following for a 301 redirect. As well, thank you for the information on the limitation of the URI:path prefix in SOL9952. I believe for our application of this iRule, I don’t foresee this as a problem, but will test to be sure.

when HTTP\_REQUEST {

if { [URI::path [HTTP::path]] eq “/” and [HTTP::query] ne “”} {

HTTP::respond 301 “Location” “http://[HTTP::host]/guest/en/home.shtml?[HTTP::query]”

}

}

---

<div class="post-metadata">

**Author:** ![nitass](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/nitass/32/2270_2.png) [@nitass](https://community.f5.com/u/nitass)\
**Post date:** [November 14, 2010, 2:15am UTC](https://community.f5.com/t/root-domain-redirect-retaining-query-string/29321/6 "2010-11-14T02:15:26Z")

</div>

u’re welcome. 🙂

---

<div class="post-metadata">

**Author:** ![Scott\_85950](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Scott\_85950](https://community.f5.com/u/Scott_85950)\
**Post date:** [November 15, 2010, 8:51pm UTC](https://community.f5.com/t/root-domain-redirect-retaining-query-string/29321/7 "2010-11-15T20:51:52Z")

</div>

Update; with further testing, we found that there were JavaScript errors generated by new queries being added to the URL when navigating in the site. We’ve further revised the iRule and are testing again to verify conflicts.

when HTTP\_REQUEST {

if { [HTTP::path] eq “/” and [HTTP::query] ne “”} {

HTTP::respond 301 “Location” “http://[HTTP::host]/guest/en/home.shtml?[HTTP::query]”

}

}
