Forum Discussion

Bob_10976's avatar
Bob_10976
Icon for Nimbostratus rankNimbostratus
Jun 22, 2011

Converting Regex to iRule

I've been tasked with moving our ISAPI rules from the web server to the LTM iRules. I understand ISAPI use regex, however I'm not familiar with that as I need to be so I was hoping to get some feedback here on converting these rules. I'm trying to understand exactly what this rule is doing. It seems as if it’s simply rewriting the host header to show without the www, cms, or even the test portion of the URI. But that doesn't seem to make a lot of sense since the hostheader would need that info to direct the end user to the correct website in IIS. But I could be looking over the obvious.

 

 

I have the following rule in place, which I thought did the same thing as the ISAPI rule, however it just simply redirects me to my public production site.

 

 


when HTTP_REQUEST {
switch "[string tolower [HTTP::host]]" {
"test.sitename.us.com" -
"cms.test.sitename.us.com" {
HTTP::header replace "sitename.us.com"
}
}
}

We are using W2K3 servers, IIS6, and Microsoft's Content Management Server. the cms in the URL is where end users, with proper permissions, go to access the content management interface to update their website, and my LTM is version 10.2.0 HF1.

RewriteCond Host: www\.test\.sitename\.us\.com

RewriteHeader Host: .* sitename\.us\.com

RewriteCond Host: cms\.test\.sitename\.us\.com

RewriteHeader Host: .* sitename\.us\.com

Thanks,

Bob

  • Hi Bob,

    If you do want to rewrite the Host header, you'd need to make a small correction to specify the header name of Host:

    
    when HTTP_REQUEST {
    switch "[string tolower [HTTP::host]]" {
    "test.sitename.us.com" -
    "cms.test.sitename.us.com" {
    HTTP::header replace Host "sitename.us.com"
    }
    }
    }
    

    Aaron
  • Thanks Aaron, however that seem to change anything. I'm still being redirected to my production website instead of the content mangement interface of the test site, and I've only have this applied to my test site, which makes it even stanger.

    I'm attempting to add in some logging so I can confirm that its at least seeing it, and maybe try to figure out when its applying the rule, however i'm doing something wrong because its saying wrong agr for header, any thoughts on what I'm missing here:

    
    
    when HTTP_REQUEST {
        switch "[string tolower [HTTP::host]]" {
            "test.sitename.us.com" -
            "cms.test.sitename.us.com" {
    log local0. before:[HTTP::header]
                HTTP::header replace Host "sitename.us.com"
    log local0. after:[HTTP::header]
            }
        }
    }
    
    
    

    Thanks,

    Bob
  • It depends on which information you are wanting from the headers:

     

     

     

    when HTTP_REQUEST {

     

    switch "[string tolower [HTTP::host]]" {

     

    "test.sitename.us.com" -

     

    "cms.test.sitename.us.com" {

     

    log local0. "Before: [HTTP::header names]"

     

    HTTP::header replace Host "sitename.us.com"

     

    log local0. "After: [HTTP::header names]"

     

    }

     

    }

     

    }

     

     

     

    Look here at Hoolio's Post for more information on logging HTTP Headers: http://devcentral.f5.com/wiki/default.aspx/iRules/LogHttpHeaders.html

     

  • I'd guess that the test application includes references to the production site in response headers and/or payload. You could use the iRule Michael referenced to troubleshoot this. Or you could use a browser plugin like Fiddler2 or HttpFox to track this. If you want to rewrite the response headers, you can use an iRule like this:

     

     

    http://devcentral.f5.com/wiki/default.aspx/iRules/RewriteHTTPRedirectHostname.html

     

     

    If you need to rewrite the response content, you can use ProxyPass:

     

     

    http://devcentral.f5.com/wiki/default.aspx/iRules/proxypassv10

     

     

    Aaron
  •  

    Thanks gents.. I believe my rule appears to be doing what its suppose to, which is rewriting the host header, I've confirmed that using the logging function, thanks for the help there.

     

     

    I think my problem is happening during the sign in process where the referring URL is getting lost. For those users that want to log in and make changes to the site via the content management interface they access a secure page first to login. With out the rule in place the Request Header reads: GET /CMS/Login.aspx?referrerUrl=http%3a%2f%2fcms.test.sitename.us.comv%2fg2p%2fK.... With the rule in place the Request Header reads: GET /CMS/Login.aspx?referrerUrl=http%3a%2f%2fsitename.us.com%2fg2p%2fK... Which is the reason why I’m being directed back to my production read only version of the site and not the test content management interface area of the site.

     

     

    So the host header getting rewritten at the LTM level is messing with the referring URL, I'm not sure there is a way around this one..

     

     

    Bob
  • You could rewrite sitename.us.com to cms.test.sitename.us.com in the URI. Here's an example you could combine with the Host header rewriting:

    
    when HTTP_REQUEST {
    
       if {[HTTP::uri] contains "sitename.us.com"}{
          HTTP::uri [string map {sitename.us.com cms.test.sitename.us.com} [HTTP::uri]]
       }
    }
    

    Aaron