Forum Discussion

JeffRay_129268's avatar
JeffRay_129268
Icon for Nimbostratus rankNimbostratus
May 26, 2015

irule issue

Hi Forum, I am having one issue of syntax, i have already an irule available for 2 requirements (1 and 2 below) which is working fine. Issue is i need to incorporate solution for the 3rd requirement below.

 

1- If anyone types HTTP based mynetwork.us then it should be redirected to HTTPS based https://www.mynetwork.us 2- If in path there is /autodiscover/autodiscover.xml then it should be redirected to autodiscover site with 301 permanent redirected message. 3- If there is / at the end of any URI then / at then end of URI should be removed and client should get back one with removed /.

 

Point 1 and 2 is working fine. Now only requirement is to add functionality of point 3 that if there is / at the end in any URI the it should be trimmed off.I have tried this code to integrate in existing one but having some syntax errors.

 

It will remove the / at end and redirect back to the user modified uri with removed /. Need your expert opinion how this will be incorporated in below detailed irule to achieve the objective.

when HTTP_REQUEST { if { [string tolower [HTTP::uri]] ends_with "/" } {

 

HTTP::redirect http://[HTTP::host][string trimright [HTTP::uri] "/"] } }

when HTTP_REQUEST { HTTP::header remove Accept-Encoding STREAM::disable redirect autodiscover requests to autodiscover site if { [string tolower [HTTP::path]] equals "/autodiscover/autodiscover.xml" and if {[string tolower [HTTP::uri] ends_with "/"]} } { HTTP::respond 301 "Location" "https://autodiscover.mynetwork.us/autodiscover/autodiscover.xml" and [string trimright [HTTP::uri] "/"] redirect http://mynetwork.us to https://www.mynetwork.us along with any uri } elseif { [string tolower [HTTP::host]] equals "mynetwork.us" AND [[HTTP::uri] ends_with "/"] } { HTTP::respond 301 "Location" "https://www.mynetwork.us" [string tolower [string trimright [HTTP::uri] "/"] } } Thanks

 

10 Replies

  • Hi JeffRay,

     

    I hope I have understood because the code is not properly formatted here.

     

    Perhaps be most interesting rewrite the uri and follow in instead of redirecting the site.

     

    Anyway, I made some adjustments and hope it works for you.

     

    Just one question:

     

    Did you have www.mynetwork.us in HTTP?

     

    Maybe you can change the operator from "equals" to "ends_with" at line:
    } elseif { [string tolower [HTTP::host]] equals "mynetwork.us" } {

     

    Regards

     

    when HTTP_REQUEST { 
        HTTP::header remove Accept-Encoding 
        STREAM::disable 
    
         redirect autodiscover requests to autodiscover site https
        if { [string tolower [HTTP::path]] equals "/autodiscover/autodiscover.xml" } {
            HTTP::respond 301 Location https://autodiscover.mynetwork.us/autodiscover/autodiscover.xml
    
         redirect mynetwork.us to https://www.mynetwork.us along with any uri
        } elseif { [string tolower [HTTP::host]] equals "mynetwork.us" } { 
            HTTP::respond 302 Location https://www.mynetwork.us[string trimright [HTTP::uri] "/"]
    
         redirect if uri ends with /
        } elseif { [HTTP::uri] ends_with "/" } {
            HTTP::respond 302 Location http://[HTTP::host][string trimright [HTTP::uri] "/"]
        }
    }
    
  • Hey claudio many thanks for your quick response and inputs. Regarding your question, requirement is if user types only mynetwork.us then it should redirect to https://www.mynetwork.us.

     

    However with your above verified code, can you confirm if all 3 mentioned below requirements are met ? I have not tested it yet so once i test it will update you accordingly. Thanks once again for your valuable input.

     

    1- If anyone types HTTP based mynetwork.us then it should be redirected to HTTPS based https://www.mynetwork.us .

     

    2- If in path there is /autodiscover/autodiscover.xml then it should be redirected to autodiscover site with 301 permanent redirected message.

     

    3- If there is / at the end of any URI then / at then end of URI should be removed and client should get back one with removed /.

     

  • Hi, I understand that this iRule will be applied on a standard VS HTTP with stream profile assigned therefore believe that meets what you need.

    [ ]

    1- If anyone types HTTP based mynetwork.us then it should be redirected to HTTPS based https://www.mynetwork.us .

    } elseif { [string tolower [HTTP::host]] equals "mynetwork.us" } {
    HTTP::respond 302 Location https://www.mynetwork.us[string trimright [HTTP::uri] "/"] }

    2- If in path there is /autodiscover/autodiscover.xml then it should be redirected to autodiscover site with 301 permanent redirected message.

    if { [string tolower [HTTP::path]] equals "/autodiscover/autodiscover.xml" } {
    HTTP::respond 301 Location https://autodiscover.mynetwork.us/autodiscover/autodiscover.xml }

    3- If there is / at the end of any URI then / at then end of URI should be removed and client should get back one with removed /.

    } elseif { [HTTP::uri] ends_with "/" } {
            HTTP::respond 302 Location http://[HTTP::host][string trimright [HTTP::uri] "/"] }
  • Hi Claudio you are right, i have got chance to test it and it worked like a charm but it has only one issue if typed mynetwork.us then site does not work because browser puts / at the end which is removed by f5. If i type like mynetwork.us/en etc then it works. So how can we define if / is immediate after host or in between complete uri it should not be removed and only removed at end of uri ?

     

  • OK, if I understood well, needs to check if the uri is not simply "/" so don't need to be redirected.

    Please let me know if I got it.

    [ ]
    when HTTP_REQUEST { 
        HTTP::header remove Accept-Encoding 
        STREAM::disable 
    
         redirect autodiscover requests to autodiscover site https
        if { [string tolower [HTTP::path]] equals "/autodiscover/autodiscover.xml" } {
            HTTP::respond 301 Location https://autodiscover.mynetwork.us/autodiscover/autodiscover.xml
    
         redirect mynetwork.us to https://www.mynetwork.us along with any uri
        } elseif { [string tolower [HTTP::host]] equals "mynetwork.us" } { 
            HTTP::respond 302 Location https://www.mynetwork.us[string trimright [HTTP::uri] "/"]
    
         redirect if uri is not / but ends with it
        } elseif { !([HTTP::uri] equals "/") and [HTTP::uri] ends_with "/" } {
            HTTP::respond 302 Location http://[HTTP::host][string trimright [HTTP::uri] "/"]
        }
    }
    
  • Thanks Claudio for your excellent response. I have not tested it yet since waiting for change approval as soon as it is done will be able to test it. Meanwhile i have just one question that how can i incorporate my existing below switch rule in above mentioned rule or if not feasible then how to achieve below requirement in if else above statement.

    switch -glob [string tolower [HTTP::uri]] {

        "/gr/mypage.xml*" {
            set find "/gr/mypage.xml"
            set replace "/gr-mypage.xml"
            HTTP::uri [ string map -nocase "$find $replace" [HTTP::uri]]
    

    set rewrite 1 }

        "/fr/mypage.xml*" {
            set find "/fr/mypage.xml"
            set replace "/fr-sitemap.xml"
            HTTP::uri [ string map -nocase "$find $replace" [HTTP::uri]]
    

    set rewrite 1 }

  • Hi,

    If you don't want to expose the new uri path, I guess that you could place it after the redirect conditions:
    when HTTP_REQUEST { 
        HTTP::header remove Accept-Encoding 
        STREAM::disable 
    
         redirect autodiscover requests to autodiscover site https
        if { [string tolower [HTTP::path]] equals "/autodiscover/autodiscover.xml" } {
            HTTP::respond 301 Location https://autodiscover.mynetwork.us/autodiscover/autodiscover.xml
            return
         redirect mynetwork.us to https://www.mynetwork.us along with any uri
        } elseif { [string tolower [HTTP::host]] equals "mynetwork.us" } { 
            HTTP::respond 302 Location https://www.mynetwork.us[string trimright [HTTP::uri] "/"]
            return
         redirect if uri is not / but ends with it
        } elseif { !([HTTP::uri] equals "/") and [HTTP::uri] ends_with "/" } {
            HTTP::respond 302 Location http://[HTTP::host][string trimright [HTTP::uri] "/"]
            return
        }
    
        switch -glob [string tolower [HTTP::uri]] {
            "/gr/mypage.xml*" {
                set find "/gr/mypage.xml"
                set replace "/gr-mypage.xml"
            }
            "/fr/mypage.xml*" {
                set find "/fr/mypage.xml"
                set replace "/fr-sitemap.xml"
            }
            default {
                set find ""
                set replace ""
            }
        }
        if { $replace ne "" } {
            set rewrite 1
            HTTP::uri [string map -nocase "$find $replace" [HTTP::uri]]
            unset find replace
        }
    }
    
  • Hi Claudio thanks for your reply, i have tried this rewrite but for some reason it seems rewrite is not working. I have tested through wireshark on server and getting gr/mypage.xml in Get instead of gr-mypage.xml. For return command after http::respond seriously not sure.

     

    • cjunior's avatar
      cjunior
      Icon for Nacreous rankNacreous
      If the request does not fall under the redirect conditions, it should rewrite the URI and deliver it to the servers. Please, add log line eg: log local0. "URI: [HTTP::uri]" below that line HTTP::uri [string map -nocase "$ find $ replace" [HTTP :: uri]] and see the LTM log eg: "tail -f /var/log/ltm" [ ]
  • I have tested through wireshark on server and getting gr/mypage.xml in Get instead of gr-mypage.xml.

     

    can you add some log command in the irule to see if it matches?