Technical Forum
Ask questions. Discover Answers.
cancel
Showing results for 
Search instead for 
Did you mean: 

iRule to Add Trailing Slash Except for Extensions

jonathanw84
Cirrus
Cirrus

Hello,

I have an iRule that I am using to direct requests to a specific pool based on the URI:

when HTTP_REQUEST {
   set uri [string tolower [HTTP::uri]]
   if {[HTTP::uri] starts_with "/apps"} {
   pool ProxyPass_DEV_pool_2
   }
   else {
   pool ProxyPass_DEV_pool_1
}
}

This is working fine, but I also need to ensure that any request that does not have an trailing slash "/" at the end, gets it added, with the exception of requests that end in an extension. For example: www.website.com/apps would become www.website.com/apps/, but for any URI really.

Additionally, www.website.com/test.html would not be affected.

Any suggestions? Thanks!

1 ACCEPTED SOLUTION

cjunior
Nacreous
Nacreous

Hello,

Considering that the resource will only have the dot char to extensions, I do this way:

when HTTP_REQUEST {
    if { not ( [HTTP::path] ends_with "/") && not ( [URI::basename [HTTP::uri]] contains "." ) } {
        # Append slash and keep querystring when it exists
        HTTP::uri [HTTP::path]/[expr { "[URI::query [HTTP::uri]]" eq {} ? {} : "?[URI::query [HTTP::uri]]" }]
    }
    if { [string tolower [HTTP::uri]] starts_with "/apps" } {
        pool ProxyPass_DEV_pool_2
    }
    else {
        pool ProxyPass_DEV_pool_1
    }
}

Where:

  1. "/apps" will become "/apps/" internally;
  2. "/apps?param=value" will become "/apps/?param=value" internally;
  3. "/test.html" and "/test.html?param=value" remais the same. 

I hope it helps.

Regards

View solution in original post

4 REPLIES 4

cjunior
Nacreous
Nacreous

Hello,

Considering that the resource will only have the dot char to extensions, I do this way:

when HTTP_REQUEST {
    if { not ( [HTTP::path] ends_with "/") && not ( [URI::basename [HTTP::uri]] contains "." ) } {
        # Append slash and keep querystring when it exists
        HTTP::uri [HTTP::path]/[expr { "[URI::query [HTTP::uri]]" eq {} ? {} : "?[URI::query [HTTP::uri]]" }]
    }
    if { [string tolower [HTTP::uri]] starts_with "/apps" } {
        pool ProxyPass_DEV_pool_2
    }
    else {
        pool ProxyPass_DEV_pool_1
    }
}

Where:

  1. "/apps" will become "/apps/" internally;
  2. "/apps?param=value" will become "/apps/?param=value" internally;
  3. "/test.html" and "/test.html?param=value" remais the same. 

I hope it helps.

Regards

Thanks cjunior! This worked well with only one minor issue. It looks like it is appending the / regardless if it the / is there or not. Is there a way to make the statement to say "if not contains "." AND does not end with "/", to add the "/"?

 

Thanks!

Ops! That passed by my eyes :/.

Thank you for warn me.

I rewrited that condition. Check that out.

Thank you so much! I was on the right track but I was slightly off. I really appreciate your help. Take care!