Forum Discussion

MihirP1129's avatar
MihirP1129
Icon for Nimbostratus rankNimbostratus
Jul 03, 2019

iRule to add www without changing URL

Hello F5 community,

 

Is there a way to add www in front of the URL while keeping the URL intact? For example:

 

https://abc.com/testfolder/blahblahblah changed to https://WWW.abc.com/testfolder/blahblahblah..

 

Thanks.

  • Do you want to change Host header sent from the client in server side connection or do you want to redirect the client to  https://WWW.abc.com/testfolder/blahblahblah

     

    to redirect the client, use following code

     

    when HTTP_REQUEST {
        if {[HTTP::host] eq "abc.com"} {
            HTTP::redirect "https://www.abc.com[HTTP::uri]"
        }
    }

     

  • JG's avatar
    JG
    Icon for Cumulonimbus rankCumulonimbus

    Probably a description of the problem you are trying to solve would help here.

    • MihirP1129's avatar
      MihirP1129
      Icon for Nimbostratus rankNimbostratus

      I don't know the full details, but what I was told from our web developers, is that this is related to an SSO authentication between the client and us. Client host a site on their end which the user authenticate to, then they click on a link on the page that takes them to a URL we host (let's say we host abc.com). So the minute the user clicks on the link, it'll send a request for https://abc.com/blahblahappenduserID (it'll append a unique ID or info to the URL) but the SSO authentication is failing, because it needs to be https://WWW.abc.com/blahblahappenduserID. I'm trying to figure out a way to add the www for the incoming request without changing the URL. Hope that makes sense.

  • Depends what you want.. if you expicitly what to replace "abc.com" with "www.abc.com" you can use this iRule.

    when HTTP_REQUEST {
        if {[HTTP::host] eq "abc.com"} {
            HTTP::header replace "Host" "www.abc.com"
        }
    }

    If you want to add "www" to any host that doesn't have "www" then that's a different request, so a bit more context would be helpful.

    Lee

    • MihirP1129's avatar
      MihirP1129
      Icon for Nimbostratus rankNimbostratus

      Hi Lee, please see my response above of what I'm trying to accomplish.

      • Lee_Sutcliffe's avatar
        Lee_Sutcliffe
        Icon for Nacreous rankNacreous

        Have you tried your code? Reading your description above, it sounds like the iRule you supplied should work. Changing the incoming host header from abc.com to www.abc.com will leave the URi/Path intact

  • Do you want to change Host header sent from the client in server side connection or do you want to redirect the client to  https://WWW.abc.com/testfolder/blahblahblah

     

    to redirect the client, use following code

     

    when HTTP_REQUEST {
        if {[HTTP::host] eq "abc.com"} {
            HTTP::redirect "https://www.abc.com[HTTP::uri]"
        }
    }

     

    • MihirP1129's avatar
      MihirP1129
      Icon for Nimbostratus rankNimbostratus

      I tried this irule and it didn't work, because when I try to hit abc.com/blahblahbalhID, nothing happened.

  • To complicate things even more, I found out that the web developer has a redirect hardcoded in the code for http://abc.com or http://www.abc.com to go to https://www.abc.com/web/default.aspx. I added the redirect iRule (Stanislas Piron) mentioned above to the 443 VS on the F5, and when I test a site https://abc.com/web/default.aspx?frm=test-option3, I get a "Hmm.. can't reach this page" in firefox. It shows Protocol (Pending) and Result (Pending) when do a Network debugging in FF when hitting that site. The redirect in the code has to stay for the site to work properly. I'm not sure how to make this work. Maybe conflicts in multiple redirects? looping?

    • MihirP1129's avatar
      MihirP1129
      Icon for Nimbostratus rankNimbostratus

      The order in which the irule was placed on the F5 was the issue. After moving the irule to the bottom of the list, it worked! Thanks everyone!