# Appending iRule

**URL:** <https://community.f5.com/t/appending-irule/28652>\
**Category:** Technical Forums\
**Tags:** dev, irules, devops, application-delivery\
**Created:** [June 10, 2009, 6:06pm UTC](https://community.f5.com/t/appending-irule/28652 "2009-06-10T18:06:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![slomah\_85788](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@slomah\_85788](https://community.f5.com/u/slomah_85788)\
**Post date:** [June 10, 2009, 6:06pm UTC](https://community.f5.com/t/appending-irule/28652/1 "2009-06-10T18:06:46Z")

</div>

Hi,

I have requested to create iRule which will add appending to url, as the customer will write ([http://mycompany.com](http://mycompany.com) ) and the application won’t work unless you write the following ([http://mycompany.com/test/app.aspx](http://mycompany.com/test/app.aspx) )

1) I created the following irule but the pictures on the page doesn’t appear

when HTTP\_REQUEST {

if { not ([HTTP::uri] starts\_with “/test”) } {

HTTP::uri /test/app.aspx [HTTP::uri]

}

}

2) I used redirection irule like this and it works but I don’t know if this will affect the application

when HTTP\_REQUEST {

if {

[HTTP::uri] equals “” or [HTTP::uri] equals “/” } {

HTTP::redirect [http://mycompany.com/test/app.aspx](http://mycompany.com/test/app.aspx) }}

I would like if someone help me in No.1 to make work.

---

<div class="post-metadata">

**Author:** ![Joe\_Pruitt](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/joe_pruitt/32/8438_2.png) [@Joe\_Pruitt](https://community.f5.com/u/Joe_Pruitt)\
**Post date:** [June 11, 2009, 5:03pm UTC](https://community.f5.com/t/appending-irule/28652/2 "2009-06-11T17:03:11Z")

</div>

For the first iRule, let’s say your incoming URI is “/foobar”. You are resetting the URI to the “/test/app.aspx /foobar”. I’m assuming that isn’t what you want to do. If you just want to change the URI to /test/app.aspx" then leave out your trailing [HTTP::uri] from the assignment.

-Joe

---

<div class="post-metadata">

**Author:** ![slomah\_85788](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@slomah\_85788](https://community.f5.com/u/slomah_85788)\
**Post date:** [June 11, 2009, 11:48pm UTC](https://community.f5.com/t/appending-irule/28652/3 "2009-06-11T23:48:40Z")

</div>

You mean to remove [HTTP::uri] and make like this:

when HTTP\_REQUEST {

if { not ([HTTP::uri] starts\_with “/test”) } {

HTTP::uri /test/app.aspx

}

}

But this gives the same results as the pictures didn’t appear.

---

<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:** [June 15, 2009, 9:30am UTC](https://community.f5.com/t/appending-irule/28652/4 "2009-06-15T09:30:49Z")

</div>

If you just want to rewrite requests to the root document (path equals /), you could use this:

```tcl
when HTTP_REQUEST {  
     Check if path is /  
    if {[HTTP::path] eq "/"}{  
  
        Rewrite path to /test/app.aspx 
       HTTP::path "/test/app.aspx"  
    }  
 }

```

If you want to prepend a string to all URIs if it’s not there already, you can use a rule like this:

```tcl
when HTTP_REQUEST {  
     Check if path doesn't already start with /test/ 
    if { not ([HTTP::path] starts_with "/test/")}{  
  
        Rewrite path by prepending "/test/" 
       HTTP::path "/test/[HTTP::path]"  
    }  
 }

```

Note that if you don’t care about inspecting the query string it should be more efficient to modify just the path using HTTP::path.

Aaron

---

<div class="post-metadata">

**Author:** ![Matt\_108113](https://avatars.discourse-cdn.com/v4/letter/m/ec9cab/32.png) [@Matt\_108113](https://community.f5.com/u/Matt_108113)\
**Post date:** [June 15, 2009, 1:58pm UTC](https://community.f5.com/t/appending-irule/28652/5 "2009-06-15T13:58:57Z")

</div>

All,

I have an append/redirect question.

Right now I am doing a straight redirect based on two different paths, but now my users want to add arguments to the paths how can I do this? Below is the current rule and an example of the url the users are trying to use.

when HTTP\_REQUEST {

if { [HTTP::uri] equals “/dev” } {

log local0. “Found the uri /”

HTTP::redirect “[https://www.server1.com:4444/dev](https://www.server1.com:4444/dev)”

}

if { [HTTP::uri] equals “/test” } {

log local0. “Found the uri /”

HTTP::redirect “[https://www.server1.com:4444/test](https://www.server1.com:4444/test)”

}

}

so basically the redirection takes you to the apache server running on 4444 instead of 443, but the question is if a user hit [https://www.server1.com/test/USERNAME](https://www.server1.com/test/USERNAME) -or- [https://www.server1.com/dev/PROJECT](https://www.server1.com/dev/PROJECT) the redirect takes them to the mainpage (since that is what the rule tells it to do), but how can I grab the PROJECT code or the USERNAME and pass that to the redirect or append it to the redirect?

Thanks,

Matt

---

<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:** [June 15, 2009, 3:18pm UTC](https://community.f5.com/t/appending-irule/28652/6 "2009-06-15T15:18:03Z")

</div>

Hi Matt,

It would help if you created a new post for your question. You can parse the fields in the path using getfield:

[getfield “/path1/path2/path3” “/” 3] returns path2

So [getfield [HTTP::path] “/” 3] for a request to /test/USERNAME would return USERNAME.

If you create a new post, can you add a few examples of what you’d like the original and modified requests to look like?

Thanks,

Aaron
