Forum Discussion

Mohammed_Akram's avatar
Mohammed_Akram
Icon for Altostratus rankAltostratus
Jun 25, 2019

request needs to redirect from https://www.example.com to https://example.com/xyz

I needs to redirect the URL if a user enters https://www.example.com it should redirect to https://example.com/xyz , I have http to https irule applied on 80.

12 Replies

  • Mohamed,

    if you are looking for a static redirect then this should work

    when HTTP_REQUEST {
         if {[string tolower [HTTP::host]] ends_with "example.com"} {
             HTTP::redirect "https://example.com/xyz"
         }
    }
    • Hamish's avatar
      Hamish
      Icon for Cirrocumulus rankCirrocumulus

      If www.example.com and example.com are the same VS, then this is the source of your looped redirect. You hit the same host again (example.com) and then redirect because your HOST ends with example.com...

      You need to check the the URI is NOT /xyz already...

      e.g

      when HTTP_REQUEST {
        if {  ([HTTP::uri] begins_with "/xyz") } {
          return
        }
        if {[string tolower [HTTP::host]] ends_with "example.com" }  {
               HTTP::redirect "https://example.com/xyz"
        }
      }

      Change begins_with to equals depending on what the rest of the content looks like...

      • Hamish's avatar
        Hamish
        Icon for Cirrocumulus rankCirrocumulus

        Because I was typing on the fly. It's starts_with not begins_with

  • If you are already doing an HTTP --> HTTPS redirect then this irule should only be added to your HTTPS Virtual server. Hope that helps

  • nathe's avatar
    nathe
    Icon for Cirrocumulus rankCirrocumulus

    Mohammed,

     

    Plenty of information/examples if you search for HTTP::redirect command in DevCentral.

     

    Here's the documentation for HTTP::redirect to get you started.

     

    N

  • Thanks for the reply

     

    all are working except if a user add www. its giving error . For the URL as am using wildcard cert

     

    http://example.com or https://example.com or example.com or

     http://www.example.com or https://www.example.com

     

     

     should be redirect to https://example.com/xyz 

     

     

  • Seems like you are having a redirect loop. Can you share your VS on config on port 80 and 443?

    list ltm virtual <virtual_server>

  • Below are the config on HTTP and HTTPS VIP

     

    (HTTP_redirected)

    list ltm virtual test-directory-redir

     

    ltm virtual test-directory-redir {

      creation-time 2019-06-17:13:17:31

      destination X.X.X.X.:http

      ip-protocol tcp

      last-modified-time 2019-06-18:23:46:04

      mask 255.255.255.255

      profiles {

        http { }

        tcp { }

      }

      rules {

        _sys_https_redirect

      }

      source 0.0.0.0/0

      source-address-translation {

        type automap

      }

      translate-address enabled

      translate-port enabled

      vs-index 43

    }

     

    ___________________________________________________________________________________________________________

     

    (HTTPS)

     

    list ltm virtual test-directory

     

    ltm virtual test-directory {

      creation-time 2019-06-17:13:15:56

      destination X.X.X.X:https

      ip-protocol tcp

      last-modified-time 2019-06-18:23:34:03

      mask 255.255.255.255

      persist {

        dest_addr {

          default yes

        }

      }

      policies {

        Gzip { }

      }

      pool Elaa_FII_Pool

      profiles {

        Elaa_Client_SSL {

          context clientside

        }

        http { }

        httpcompression { }

        tcp { }

      }

      rules {

        X-Forwarded-For

        ends_with_www

      }

      source 0.0.0.0/0

      source-address-translation {

        type automap

      }

      translate-address enabled

      translate-port enabled

      vs-index 42

    }