# Replace subdomain and remove path

**URL:** <https://community.f5.com/t/replace-subdomain-and-remove-path/31445>\
**Category:** Technical Forums\
**Tags:** irules, devops, application-delivery\
**Created:** [January 28, 2014, 3:26pm UTC](https://community.f5.com/t/replace-subdomain-and-remove-path/31445 "2014-01-28T15:26:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olivier\_Minet\_6](https://avatars.discourse-cdn.com/v4/letter/o/e19b73/32.png) [@Olivier\_Minet\_6](https://community.f5.com/u/Olivier_Minet_6)\
**Post date:** [January 28, 2014, 3:26pm UTC](https://community.f5.com/t/replace-subdomain-and-remove-path/31445/1 "2014-01-28T15:26:36Z")

</div>

Greetings,

I’m struggling with this :

I would like this : **[http://www.mysite.org/customfiles/mydoc.htm](http://www.mysite.org/customfiles/mydoc.htm)** transformed into this: **[http://customfiles.mysite.org/mydoc.htm](http://customfiles.mysite.org/mydoc.htm)**

Of course, there are several files, mydoc.htm is just an example. And the original site [www.mysite.org](http://www.mysite.org) must not be affected.

Note : This URL will be forwarded to a pool so I cannot use redirect.

I’ve written the following iRule:

```tcl
when HTTP_REQUEST {
if { [HTTP::path] starts_with "/customfiles"} {
  HTTP::path [string replace [HTTP::path] 0 12 ""]
  HTTP::header replace Host "customfiles.onprvp.fgov.be"
  pool my_pool
}
}

```

And nothing happens. In the F5 logs we only have a warning message saying the rule already exists on partition Common, but nothing else.

I suspect that the string replace isn’t very happy but a string map gives the same.

What am I doing wrong ? 🙂

---

<div class="post-metadata">

**Author:** ![nathe](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@nathe](https://community.f5.com/u/nathe)\
**Post date:** [January 28, 2014, 3:44pm UTC](https://community.f5.com/t/replace-subdomain-and-remove-path/31445/2 "2014-01-28T15:44:13Z")

</div>

Olivier, by no means an expert but I’d use string range instead e.g.

```tcl
HTTP::path [string range [HTTP::path] 0 12]

```

Does this work for you?

N

---

<div class="post-metadata">

**Author:** ![Arie](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Arie](https://community.f5.com/u/Arie)\
**Post date:** [January 28, 2014, 5:02pm UTC](https://community.f5.com/t/replace-subdomain-and-remove-path/31445/3 "2014-01-28T17:02:43Z")

</div>

It may be beneficial to verify that hyperlinks on those pages will continue to point to valid URLs.

---

<div class="post-metadata">

**Author:** ![IheartF5\_45022](https://avatars.discourse-cdn.com/v4/letter/i/3be4f8/32.png) [@IheartF5\_45022](https://community.f5.com/u/IheartF5_45022)\
**Post date:** [January 29, 2014, 6:34am UTC](https://community.f5.com/t/replace-subdomain-and-remove-path/31445/4 "2014-01-29T06:34:34Z")

</div>

I think counting may be the problem :-);

```tcl
when HTTP_REQUEST {
    if { [HTTP::path] starts_with "/customfiles"} {
        HTTP::uri [string replace [HTTP::path] 0 11 ""]
        HTTP::header replace Host "customfiles.onprvp.fgov.be"
        pool my_pool
    }
}

```

---

<div class="post-metadata">

**Author:** ![Olivier\_Minet\_6](https://avatars.discourse-cdn.com/v4/letter/o/e19b73/32.png) [@Olivier\_Minet\_6](https://community.f5.com/u/Olivier_Minet_6)\
**Post date:** [February 4, 2014, 12:34pm UTC](https://community.f5.com/t/replace-subdomain-and-remove-path/31445/5 "2014-02-04T12:34:36Z")

</div>

Hello all,

Thanks very much for your answers.

I have tried your suggestions but unfortunately it does not work ☹

I keep on searching and keep you informed of the results.

Olivier

(Sorry for my late answer, just came back from a few days off)

---

<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:** [February 4, 2014, 12:52pm UTC](https://community.f5.com/t/replace-subdomain-and-remove-path/31445/6 "2014-02-04T12:52:01Z")

</div>

I have tried your suggestions but unfortunately it does not work ☹

have you run tcpdump to see what wrong is?

---

<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:** [February 4, 2014, 12:55pm UTC](https://community.f5.com/t/replace-subdomain-and-remove-path/31445/7 "2014-02-04T12:55:07Z")

</div>

e.g.

```tcl
config

root@(ve11a)(cfg-sync In Sync)(Active)(/Common)(tmos) list ltm virtual bar
ltm virtual bar {
    destination 172.28.24.10:80
    ip-protocol tcp
    mask 255.255.255.255
    pool foo
    profiles {
        http { }
        tcp { }
    }
    rules {
        myrule
    }
    source 0.0.0.0/0
    source-address-translation {
        type automap
    }
    vs-index 2
}
root@(ve11a)(cfg-sync In Sync)(Active)(/Common)(tmos) list ltm pool foo
ltm pool foo {
    members {
        200.200.200.101:80 {
            address 200.200.200.101
        }
    }
}
root@(ve11a)(cfg-sync In Sync)(Active)(/Common)(tmos) list ltm rule myrule
ltm rule myrule {
    when HTTP_REQUEST {
  if { [HTTP::path] starts_with "/customfiles/" } {
    HTTP::uri [string map {/customfiles/ /} [HTTP::uri]]
    HTTP::header replace Host "customfiles.mysite.org"
  }
}
}

 trace

[root@ve11a:Active:In Sync] config ssldump -Aed -nni 0.0 port 80
New TCP connection 1: 172.28.24.1(36831) <-> 172.28.24.10(80)
1391521795.0090 (0.0011) C>S
---------------------------------------------------------------
GET /customfiles/mydoc.htm HTTP/1.1
User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
Accept: */*
Host: www.mysite.org

---------------------------------------------------------------

New TCP connection 2: 200.200.200.14(36831) <-> 200.200.200.101(80)
1391521795.0275 (0.0173) C>S
---------------------------------------------------------------
GET /mydoc.htm HTTP/1.1
User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
Accept: */*
Host: customfiles.mysite.org

---------------------------------------------------------------

```

---

<div class="post-metadata">

**Author:** ![IheartF5\_45022](https://avatars.discourse-cdn.com/v4/letter/i/3be4f8/32.png) [@IheartF5\_45022](https://community.f5.com/u/IheartF5_45022)\
**Post date:** [February 4, 2014, 9:54pm UTC](https://community.f5.com/t/replace-subdomain-and-remove-path/31445/8 "2014-02-04T21:54:26Z")

</div>

and logging;-

```tcl
when HTTP_REQUEST {
    if { [HTTP::path] starts_with "/customfiles"} {
        log local0. "Replacing [HTTP::uri] with [string replace [HTTP::path] 0 11 ""]"
        HTTP::uri [string replace [HTTP::path] 0 11 ""]
        log local0. "Replaced - \[HTTP::uri\] is [HTTP::uri]"
        HTTP::header replace Host "customfiles.onprvp.fgov.be"
        pool my_pool
    }
}

```
