Forum Discussion

SK74_347537's avatar
SK74_347537
Icon for Nimbostratus rankNimbostratus
Mar 15, 2018

Replacing the protocol from http to https for certain domain

I am trying to replace the protocol of the Location from http to https for certain domains(url) with iRules but getting error with it.

when HTTP_RESPONSE {
    if { ( [HTTP::header exists Location] ) and ( [HTTP::header Location] contains "http://" ) and ( class match [HTTP::header Location] contains ssl-domain-data-group) } {
        HTTP::header replace [string map -nocase {"http://" "https://"} [HTTP::header Location]]
    }
}

Is the syntax wrong? I am new to iRules, so any help is greatly appreciated.

7 Replies

  • oguzy's avatar
    oguzy
    Icon for Cirrostratus rankCirrostratus

    Hi SK74,

    class match filter should be in brackets like this.

        when HTTP_RESPONSE {
            if { ( [HTTP::header exists Location] ) and ( [HTTP::header Location] contains "http://" ) and ( [ class match [HTTP::header Location] contains ssl-domain-data-group ] ) } {
                HTTP::header replace [string map -nocase {"http://" "https://"} [HTTP::header Location]]
            }
        }
    

    Also you can try the below one:

        when HTTP_RESPONSE {
            if { [HTTP::header exists Location] } {
               set location_info [HTTP::header Location]
               if { ( $location_info contains "http://" ) and ( [class match $location_info contains ssl-domain-data-group ] ) } {
                   HTTP::header replace [string map -nocase {"http://" "https://"} $location_info]]
               }
            }
        }
    
    • SK74_347537's avatar
      SK74_347537
      Icon for Nimbostratus rankNimbostratus

      Problem solved! Thanks for the help, really appreciate it. Need to add "Location" before replace to correctly modify the HTTP header.

      when HTTP_RESPONSE {
          if { ( [HTTP::header exists Location] ) and ( [HTTP::header Location] contains "http://" ) and ( [ class match [HTTP::header Location] contains ssl-domain-data-group ] ) } {
              HTTP::header Location replace [string map -nocase {"http://" "https://"} [HTTP::header Location]]
          }
      }
      
  • Hi SK74,

    class match filter should be in brackets like this.

        when HTTP_RESPONSE {
            if { ( [HTTP::header exists Location] ) and ( [HTTP::header Location] contains "http://" ) and ( [ class match [HTTP::header Location] contains ssl-domain-data-group ] ) } {
                HTTP::header replace [string map -nocase {"http://" "https://"} [HTTP::header Location]]
            }
        }
    

    Also you can try the below one:

        when HTTP_RESPONSE {
            if { [HTTP::header exists Location] } {
               set location_info [HTTP::header Location]
               if { ( $location_info contains "http://" ) and ( [class match $location_info contains ssl-domain-data-group ] ) } {
                   HTTP::header replace [string map -nocase {"http://" "https://"} $location_info]]
               }
            }
        }
    
    • SK74_347537's avatar
      SK74_347537
      Icon for Nimbostratus rankNimbostratus

      Problem solved! Thanks for the help, really appreciate it. Need to add "Location" before replace to correctly modify the HTTP header.

      when HTTP_RESPONSE {
          if { ( [HTTP::header exists Location] ) and ( [HTTP::header Location] contains "http://" ) and ( [ class match [HTTP::header Location] contains ssl-domain-data-group ] ) } {
              HTTP::header Location replace [string map -nocase {"http://" "https://"} [HTTP::header Location]]
          }
      }
      
  • you can use multiple value pair in string map command:

    when HTTP_RESPONSE {
        if { [HTTP::header exists Location] } {
            HTTP::header replace [string map -nocase {"http://www.example.com" "https://www.example.com" "http://www2.example.com" "https://www2.example.com" "http://www3.example.com" "https://www3.example.com" "http://www4.example.com" "https://www4.example.com"} [HTTP::header Location]]
        }
    }
    
  • The other way around. Stripping the "http" from the location and replacing it with "https".