Forum Discussion

Anish_Srivastav's avatar
Anish_Srivastav
Icon for Nimbostratus rankNimbostratus
Mar 02, 2010

URL host rewrite based on URI

Hello guys

 

 

I was browsing through the forum and could not come across a similar situation that I am facing. We have a website with following links:

 

 

http://www.xyz.com/Product1

 

http://www.xyz.com/Product2

 

http://www.xyz.com/Product3

 

 

www.xyz.com is setup as a virtual server on the F5. Also, the above URL's have been indexed by various search engines. Now, we want to do the following:

 

 

If the request comes to http://www.xyz.com/Product1 then it should get redirected to http://Product1.xyz.com/Product1, now this is not that much of a problem as I can achieve this by setting a Host Header value Product1.xyz.com on the IIS webserver and an irule on the LTM such as:

 

 

when HTTP_REQUEST {

 

if {([HTTP::uri] starts_with "/Product1" and [HTTP::host] equals "www.xyz.com")}

 

{

 

HTTP::response 302 location "http://Product1.xyz.com[HTTP::uri]"

 

}

 

}

 

 

But, now from this page if the subsequent request is to /Product2, the link in the browser shows http://Product1.xyz.com/Product2. I would like the link in this case to be http://Product2.xyz.com/Product2 or in case of Product3 http://Product3.xyz.com/Product3

 

 

Is there any simpler way to achieve this rather than multiple if-else statements in the iRule as the Product line will keep on increasing in the future.

 

 

TIA

 

 

Anish

3 Replies

  • Hi Anish,

     

     

    Can you give a few more exact examples of when you want to redirect the client request?

     

     

    Is the general logic, check the first directory in the URI (/product1/ or /product2/) and see if it starts with the literal string "product" and then check if it does not match the hostname's first field (product1.xyz.com)?

     

     

    I assume "product" isn't the actual word you want to look for. But is that word always the same that you want to look for in both the URI and host header value?

     

     

    Thanks,

     

    Aaron
  • Hey Aaron

     

     

    Let me try to explain again

     

     

    The main domain of the site www.xyz.com is setup on the f5 as a virtual server and it has various sub-sections. One of the sections is product1, which usually users can access as http://www.xyz.com/product1 and this is also how its indexed in various search engines as well.

     

     

    Let's say for SEO purposes I needed to create a sub-domain product1.xyz.com with a host header entry in IIS so that requests to http://www.xyz.com and http://product1.betus.com return the same response i.e. content and http://www.xyz.com/product1 is the same as http://product1.xyz.com/product1 (product1 is a subdirectory in the WebRoot).

     

     

    So, basically what I am trying to achive is that when a request comes to /product1 on host http://www.xyz.com/[HTTP::uri] it should get redirected to http://product1.xyz.com/[HTTP::uri]

     

     

    However, since, its a redirect, now any subsequent requests on the page (for e.g.) if the user clicks on /product2 (another of the subsections) the browser address bar shows http://product1.xyz.com/product2. This is what I don't want to happen. Either the Address bar should revert to the default host URL i.e. http://www.xyz.com/product2 or if I have another rule then redirect to http://product2.xyz.com/product2.

     

     

    I will create as many host-headers that are needed for product1, product2 etc.

     

     

    Hope I have not confused you any further. Thanks again

     

     

    Regards

     

    Anish
  • So maybe something like this?

     
     when HTTP_REQUEST { 
      
         Check if the subdomain starts with www. or is null 
        if {[string tolower [HTTP::host]] starts_with "www." or [HTTP::host] eq ""}{ 
      
            Exit this event in this rule 
           return 
        } 
        log local0. "Host didn't start with www: [HTTP::host]" 
      
         Save the first directory in the HTTP path, set to lower case 
        set first_dir [string tolower [getfield [HTTP::path] "/" 2]] 
      
         Check if the first directory matches some pattern like "product" 
        if {not ($first_dir starts_with "product")}{ 
      
            Exit this event in this rule 
           return 
        } 
        log local0. "\$first_dir, $first_dir, matched the pattern check" 
      
         Check if the first directory in the path is not the same as the subdomain 
        if {$first_dir ne [getfield [string tolower [HTTP::host]] "." 1]}{ 
      
              log local0. "Redirecting request to $first_dir.xyz.com" 
              HTTP::redirect "$first_dir.xyz.com" 
           } 
        } 
     } 
     

    Aaron