Forum Discussion

aandreyy_293459's avatar
aandreyy_293459
Icon for Nimbostratus rankNimbostratus
Jul 25, 2017

iRule for redirecting to different uri

iRule for redirecting to different uri

 

i have problem like have redirection in iRule when HTTP_REQUEST "/url" use pool POOL now i need have "/newurl" use pool NEWPOOL but need forward to NEWPOOL servers url like not /newurl. Can i replace this somehow? If i am adding "/newurl* "HTTP::uri "/url" , it still going to old POLL not NEWPOLL. Any suggestions? thanks

 

8 Replies

  • P_K's avatar
    P_K
    Icon for Altostratus rankAltostratus

    is this your requirement ??

     

    user -> /url --> pool pool1

     

    user -> /newurl* --> replace uri with /url --> pool newpool

     

  • regarding "user -> /newurl* --> replace uri with /url --> pool newpool"

     

    as i said adding "/newurl* "HTTP::uri "/url" , it still going to old POOL not NEWPOOL.

     

    when i replace /newurl to /url in web browser it changes to and still going to old POOL.

     

    for me it can help if i could set something like POOL1 pool1members/url

     

  • P_K's avatar
    P_K
    Icon for Altostratus rankAltostratus

    Try this..

     

    when HTTP_REQUEST {

     

    if { [ string tolower [HTTP::uri]] starts_with "/url" } {

     

    pool oldpool

     

    }

     

    elseif { [string tolower [HTTP::uri]] starts_with "/newurl" } {

     

    HTTP::uri "/url"

     

    pool NewPool

     

    }

     

    }

     

  • iRule is working, but as i said it is redirecting to old pool. Looks like it is checking after replacing uri again and findind old pool redirection.

     

    • P_K's avatar
      P_K
      Icon for Altostratus rankAltostratus

      I'm sorry.. i should've been clear.. Its actually sending me to new pool when using newurl and also replacing the /newurl with /url.

       

      Add logging to see if its sending to old pool.

       

      log local0. "sending to oldpool with [HTTP::uri]" --> add this in first if condition

       

      log local0. "sending to Newpool with [HTTP::uri]" --> add this in second if condition

       

  • hi, ok I replaced my iRule from this one above to see is it working and getting in logs

     

    "< http_request >: sending to oldpool with /url" -

     

    "< http_request >: sending to Newpool with /newurl " -

     

    looks like it is not changing newurl to "/url"? like it should do regarding this statement HTTP::uri "/url" ? or i am wrong?

     

    • aandreyy_293459's avatar
      aandreyy_293459
      Icon for Nimbostratus rankNimbostratus

      can not look into my last message because it is not true. I cleared all browser history and got the same result as with my iRules. And we are back to my first question in my first message :)

       

      it is clear from logs. HTTP::uri "/url" statement change URL and it is forwarding one time to new pool, but after that load balancer is forwarding to old pool because i can see in browser my URL was changed also from "; to . And after that load balancer is forwarding as /url to old pool.

       

      < HTTP_REQUEST >: sending to Newpool with /url

       

      < HTTP_REQUEST >: sending to oldpool with /url

       

      iRule changing also URL in browser and this is problem. So this is will not solve my problem at all. My idea that we could send to pool (pool members) changed url (not that one in browser). Not sure is it possible. My logic is like

       

      if { [ string tolower [HTTP::uri]] starts_with "/newurl" } {

       

      pool NEWPOOL

       

      }

       

      this is correct, But lets say NEWPOOL has: server1 server2

       

      so can we send to those server request adding anything like server1/url not depending on what URL we have in browser?

       

      using iRule up here we have something like URL forwarding and it could work, but we have in iRule the same statement, that's why it is not working as we need.

       

  • Hello Aandrey,

    I read you comments, I think that what you need to do is to rewrite all response coming from Newpool containing URL that points to /url.

    You need to add a "SREAM PROFILE" to your VS and use the following irule example :

    when HTTP_REQUEST {
    
            set rewrite "0" 
            set host [string tolower [HTTP::host]]
    
            if { [ string tolower [HTTP::uri]] starts_with "/url" } {
                pool oldpool
            } elseif { [string tolower [HTTP::uri]] starts_with "/newurl" } {
                set rewrite "1"
                STREAM::disable
                HTTP::header remove "Accept-Encoding"
                pool NewPool
            }
        }
    
        when HTTP_RESPONSE {
    
             Make rewrite only for responses coming from pool NewPool
            if { rewrite eq "1" } {
    
                if {[HTTP::header exists Location] } {
                    if { [string tolower [HTTP::header Location]] contains "/url/"} {
                        log local0. "Old Location is: [HTTP::header Location]"
                        HTTP::header replace Location [string map {"/url/" "/newurl/"} [HTTP::header Location]]
                        log local0. "New Location is: [HTTP::header Location]"
                    }
                }
    
                if {[HTTP::header value Content-Type] contains "text" || [HTTP::header value Content-Type] contains "xml" }{
                     Will replace all links in text or xml "Content-Type" that contains yourapp.doamin.com/url by yourapp.doamin.com/newurl
                    STREAM::expression "@$host/url@$host/newurl@"
                    STREAM::enable
                }
    
            }
    
    }
    

    Hope it helps

    Regards