Forum Discussion

THawesBPC_28376's avatar
THawesBPC_28376
Icon for Nimbostratus rankNimbostratus
May 02, 2017

Modify HTTP::Host and HTTP::URI in an iRule

I have a need to redirect requests from a subfolder, http://www.website.com/folder/a/b/ for example, to a subdomain of http://folder.website.com/a/b/. I am somewhat able to accomplish this but it doesn't solve for someone going to http://www.website.com/folder, as it takes the user to http://folder.website.com/folder which will not work.

when HTTP_REQUEST {
    if { [HTTP::uri] starts_with "/folder"} {
        set path [HTTP::uri]
        set locate [string first "/" $path 1]
        set cleanpath [string range $path $locate end]

        HTTP::redirect "http://folder.website.com$cleanpath"
        }
}
  • P_K's avatar
    P_K
    Icon for Altostratus rankAltostratus

    Is your uri's /folder/a/b/ and /a/b are static? can you explain what exactly you need? I may help out with an irule.

     

  • Thanks PK.

     

    Sorry, I should have elaborated. folders a & b are not static and will be different depending on the content selected by the user, so those will need to be extracted from the URI and carried over as part of the redirect, removing only the first directory from it (/folder/). Does that make sense?

     

  • A little modification in the irule should help

    when HTTP_REQUEST {
        if { [HTTP::uri] starts_with "/folder" && !([HTTP::uri] ends_with "/folder/")} {
            set path [HTTP::uri]
            set locate [string first "/" $path 1]
            set cleanpath [string range $path $locate end]
    
            HTTP::redirect "http://folder.website.com$cleanpath"
            log local0. "http://folder.website.com$cleanpath"
            }
            elseif { [HTTP::uri] starts_with "/folder" && [HTTP::uri] ends_with "/folder/"} {
             HTTP::redirect "http://folder.website.com"
            }
    }
    
  • P_K's avatar
    P_K
    Icon for Altostratus rankAltostratus

    Not sure but give this a try!

     

    when HTTP_REQUEST {

     

    if { [string tolower [HTTP::host]] eq "; && [HTTP::uri] starts_with "/folder"} {

     

    HTTP::uri [string map {"/folder" ""} [HTTP::uri]]

     

    HTTP::redirect "http://folder.website.com[HTTP::uri]"

     

    }

     

    elseif {[string tolower [HTTP::host]] eq "; && [HTTP::uri] eq "/folder"} {

     

    HTTP::redirect ";

     

    }

     

    }

     

  • Thanks PK & DevBabu! You were both able to lead me to the solution. I just needed to add one additional piece to solve for website.com/folder instead of website.com/folder/

    when HTTP_REQUEST {
        if { [HTTP::uri] ends_with "/folder"} {
             HTTP::redirect "http://folder.website.com"
        }
        elseif { [HTTP::uri] starts_with "/folder" && !([HTTP::uri] ends_with "/folder/")} {
            set path [HTTP::uri]
            set locate [string first "/" $path 1]
            set cleanpath [string range $path $locate end]
            HTTP::redirect "http://folder.website.com$cleanpath"
        } 
        elseif { [HTTP::uri] starts_with "/folder" && [HTTP::uri] ends_with "/folder/"} {
             HTTP::redirect "http://folder.website.com"
        }
    }