Contribute Open Script

[BRCD_OS_112] Host Header Rewrite

by Yasir_Liaqatullah on ‎01-20-2012 02:35 PM (221 Views)

Area

Content manipulation

Traffic

HTTP

Description

            This script retrieves the Host header in the incoming requests and analyzes it. If the header contains a string matching www.abc.com, it is replaced with www.xyz.com. A regular expression is used for replacing and the match criteria is *.abc.*

Performance Estimate

            TBD

Required Backend configuration

            No special configuration is required

Events and API Used

Events

API

HTTP_REQUEST

OS_HTTP_REQUEST::host

OS_SLB::forward

Script source code

# Purpose: host header rewrite www.abc.com -> www.xyz.com

use OS_SLB;

use OS_HTTP_REQUEST;

#############################################

#

# HTTP REQUEST EVENTS

#

#############################################

sub HTTP_REQUEST {

     

       # Get the URL from the request

       my $client_host = OS_HTTP_REQUEST::host;

        if ($client_host =~ /^(.+)\.abc\.(.+)$/ ) {

          

            $new_host = "$1.xyz.$2";

           # rewrite host to the new modified one

           OS_HTTP_REQUEST::host("$new_host");

        }

     

       # Choose real server based on hash of URL

       OS_SLB::forward(10);

}