Forum Discussion

mkyrc's avatar
mkyrc
Icon for Cirrus rankCirrus
Jun 22, 2021

How to replace [HTTP::host] using regex

Hi all,

I'd like to replace Host information in HTTP header with regexp because I need read dynamic part of (sub)domain.

I need change this (example):

(.*).domain.com -> %1.newdomain.com

First part of domain (3rd level in this example) is dynamic, therefore I need regex. How does the irule look like? Can anybody help me find solution? thx.

  • My solution without "regex":

    HTTP::header replace Host [string map -nocase {".domain.com" ".newdomain.com"} [HTTP::host]]

    The level 3 (or 4) subdomain is untouched. This solves my problem.

    I'd like to know solution with regex, because sometimes is helpful string manipulation like this: foo.domain.com -> foo.com (in this case is 'foo' is unknown, therefore is regex helpful).

  • Hi Martin,

     

    Let me have a look if I understand this correctly; you want to replace the Host header if the last parts of the header contain a certain domain, irrespective of what the subdomain looks like?

     

    when HTTP_REQUEST { 

      if { [HTTP::host] ends_with ".domain.com" } { 

        HTTP::host "www.blabla.com" 

      }

    }

     

    Hope this helps, but please shout if I have misunderstood your question.

     

    • mkyrc's avatar
      mkyrc
      Icon for Cirrus rankCirrus

      Thanks for reaction Alex. This is not good example.

       

      I need modify Host string from "unknown.sub.domain.com" to "unknown.nedomain.com". In other words, I need keep 'unknown' and change only end part of domain ('sub.domain.com' -> 'domain.com'). Solution for this case is below. Your example replaces Host part statically (*.domain.com -> www.newdomain.com).

       

      I'm migrating configuration from other vendor's device to f5 and there is regex used for string manipulation. Therefore my question was directed to regex. But in this case it's not needed (string map is helpful).

  • My solution without "regex":

    HTTP::header replace Host [string map -nocase {".domain.com" ".newdomain.com"} [HTTP::host]]

    The level 3 (or 4) subdomain is untouched. This solves my problem.

    I'd like to know solution with regex, because sometimes is helpful string manipulation like this: foo.domain.com -> foo.com (in this case is 'foo' is unknown, therefore is regex helpful).