Forum Discussion

Joe_Pipitone's avatar
Joe_Pipitone
Icon for Nimbostratus rankNimbostratus
Sep 30, 2015

Redirecting - avoiding redirect loop

We have a site that has many pages indexed by search engines. We want all of those pages redirected back to the root domain, removing the uri and redirecting the client to http://domain.com/home.aspx.

 

The following iRule always results in an HTTP 400 error. I'm also wondering if we can perform a redirect without resulting in a redirect loop - so the URI always redirects the client back to Home.aspx. The code also needs to strip the www.

 

Thanks for any help.

 

when HTTP_REQUEST {
    if {([string tolower [HTTP::host]] starts_with "www.")} {
        HTTP::redirect "http://[string range [HTTP::host] 4 end][HTTP::uri]"
        return
    } elseif { [TCP::local_port clientside] == 80 } {
        HTTP::uri "home.aspx"
        return
    }
}

5 Replies

  • So what you're saying is, if the client side destination port is 80 (which it always is), issue a redirect to the home.aspx URI. That will most certainly create a redirect loop. So how about something like this:

    if { not ( [string tolower [HTTP::uri]] starts_with "/home.aspx" ) } {
        HTTP::redirect "http://domain.com/home.aspx"
    }
    
  • Did you enclose it in an event?

    when HTTP_REQUEST {
        if { not ( [string tolower [HTTP::uri]] starts_with "/home.aspx" ) } {
            HTTP::redirect "http://domain.com/home.aspx"
        }
    }
    
  • One more thing - the images and css on that site all now seem to be getting redirected, so the page doesn't display correctly.

    You're probably going to have to make provisions in your logic to account for these.

    Example, if URI does not equal or contain a CSS or image resource, and does not start with /home.aspx, redirect.
    
  • It boils down to how you need to match on these URIs. Do they all fall into specific URI patterns? Do they have .css, .png, .jpg, etc. file extensions?

     

  • etc. I'm going to think about this a bit more. I thought it would be a simple redirect.

    It was before you mentioned images and css... 😉

    What I think you're basically saying is that you only want to redirect if the URL isn't for images or css. And for that you have to know WHAT constitutes one of these objects. If the URLs for images and styles always end with an image or .css extension, then it should be pretty straight forward.

    If the URI doesn't end with .png or .jpg or .gif or .css and doesn't start with /home.aspx, redirect.

    when HTTP_REQUEST {
        if { not ( [class match [string tolower [HTTP::uri]] ends_with ext-dg] ) and not ( [string tolower [HTTP::uri]] starts_with "/home.aspx" ) } {
            HTTP::redirect "http://[HTTP::host]/home.aspx"
        }
    }
    

    where ext-dg is a string-based datagroup that contains all of the extensions you want to not redirect.

    root@(bigip116)(cfg-sync Standalone)(Active)(/Common)(tmos) list ltm data-group internal ext-dg 
    ltm data-group internal ext-dg {
        records {
            .css { }
            .gif { }
            .jpg { }
            .png { }
        }
        type string
    }