Forum Discussion

Damion_19248's avatar
Damion_19248
Icon for Nimbostratus rankNimbostratus
Mar 05, 2013

redirect to new domain using a part of the uri

Sample url

 

http://subdomain.domain.com/1050/38745/productdata-reviews/reviews.htm

 

301 redirect to

 

http://domain.com/38745/

 

The subdomain, the 1050 and the last portion reviews.htm is always constant. I need the subdomain to redirect to the main domain and extract the second set of numbers(38745 in the example above) from the uri and apply that as the full uri in the new 301 redirected url.

 

10 Replies

  • e.g.

    [root@ve10:Active] config  b virtual bar list
    virtual bar {
       snat automap
       pool foo
       destination 172.28.19.252:80
       ip protocol 6
       rules myrule
       profiles {
          http {}
          tcp {}
       }
    }
    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
      if { ( [HTTP::host] equals "subdomain.domain.com" ) and
           ( [scan [HTTP::path] {/1050/%d/productdata-reviews/reviews.htm} num] == 1 ) } {
        HTTP::respond 301 noserver Location "http://domain.com/${num}/"
      }
    }
    }
    [root@ve10:Active] config  curl -I http://subdomain.domain.com/1050/12345/productdata-reviews/reviews.htm
    HTTP/1.0 301 Moved Permanently
    Location: http://domain.com/12345/
    Connection: Keep-Alive
    Content-Length: 0
    
    
  • Thanks for your response. Can you tell me what is happening with %d and num? It looks like it the value for %d is created in a variable called num. I will do some digging in the morning to see if I can get some understanding on the rule you wrote.
  • Can you tell me what is happening with %d and num? It looks like it the value for %d is created in a variable called num.yes, you are correct.

     

     

    iRules 101 - 16 - Parsing Strings with the TCL Scan Command by Jason

     

    https://devcentral.f5.com/tech-tips/articles/irules-101-16-parsing-strings-with-the-tcl-scan-command
  • The rule works for this case but will not match if another portion of the url changes. I have listed an example below. Is there a way to add a wildcard match for the data in between the next two slashes and is there also a way to store that next section in another variable? Thanks in advance for any help.

     

     

    original supplied url

     

    http://subdomain.domain.com/1050/38745/productdata-reviews/reviews.htm

     

    here is another url that would be a possibility in this case

     

    http://subdomain.domain.com/1050/346687/otherproductdata-reviews/reviews.htm
  • e.g.

    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
      if { ( [HTTP::host] equals "subdomain.domain.com" ) and
           ( [scan [HTTP::path] {/%d/%d/%s} num1 num2 str] == 3 ) } {
            HTTP::respond 301 noserver Location "http://domain.com/${num2}/"
            log local0. "client [IP::client_addr]:[TCP::client_port] | origin host [HTTP::host] | origin uri [HTTP::uri] | num1 $num1 | num2 $num2 | str $str"
      }
    }
    }
    
     test
    
    [root@ve10:Active] config  curl -I http://subdomain.domain.com/1050/38745/productdata-reviews/reviews.htm
    HTTP/1.0 301 Moved Permanently
    Location: http://domain.com/38745/
    Connection: Keep-Alive
    Content-Length: 0
    
    [root@ve10:Active] config  curl -I http://subdomain.domain.com/1050/346687/otherproductdata-reviews/reviews.htm
    HTTP/1.0 301 Moved Permanently
    Location: http://domain.com/346687/
    Connection: Keep-Alive
    Content-Length: 0
    
     log
    
    [root@ve10:Active] config  tail -f /var/log/ltm
    Mar  8 08:08:14 local/tmm info tmm[22185]: Rule myrule : client 172.28.19.253:42841 | origin host subdomain.domain.com | origin uri /1050/38745/productdata-reviews/reviews.htm | num1 1050 | num2 38745 | str productdata-reviews/reviews.htm
    Mar  8 08:08:34 local/tmm info tmm[22185]: Rule myrule : client 172.28.19.253:42842 | origin host subdomain.domain.com | origin uri /1050/346687/otherproductdata-reviews/reviews.htm | num1 1050 | num2 346687 | str otherproductdata-reviews/reviews.htm
    
  • I thought I would be able to modify what you gave me to satisfy a few other parameters but apparently I don't have a good enough understanding of irule/tcl syntax to make it work. I modified your original rule to this below.

     

    when HTTP_REQUEST { if {[HTTP::host] equals "subdomain.domain.com" and [HTTP::uri] ends_with "reviews.htm"}{ [scan [HTTP::path] /%d/%d/%s num1 num2 str] == 3}{ HTTP::respond 301 noserver Location "http://www.domain.com/${num2}/" } }

     

    The original request got expanded and I have another condition that needs to be considered as well. In the examples below, productdata and moredata(if present) are variable.

     

    http://subdomain.domain.com/1050/38745/productdata-reviews/category.htm Redirect to http://www.domain.com/productdata/

     

    http://subdomain.domain.com/1050/38745/productdata-moredata-reviews/categories.htm Redirect to http://www.domain.com/productdata-moredata/

     

     

    I attempted to add the additional condition to the already working rule but ran into syntactical problems. I realize this does't remove the necessary data from the string to but I was starting with it and then would add the portion to remove the data I don't need. I thought that would be easy since the data(-reviews/category.htm) is always constant. Could you help with the additional requirement and also tell me why the syntax of the rule below doesn't work?

     

    when HTTP_REQUEST { if {[HTTP::host] equals "subdomain.domain.com" and [HTTP::uri] ends_with "reviews.htm"}{ [scan [HTTP::path] /%d/%d/%s num1 num2 str] == 3}{ HTTP::respond 301 noserver Location "http://www.domain.com/${num2}/" } elseif {[HTTP::host] equals "subdomain.domain.com" and [HTTP::uri] ends_with "category.htm"}{ [scan [HTTP::path] /%d/%d/%s num1 num2 str] == 3}{ HTTP::respond 301 noserver Location "http://www.domain.com/${str}/" } }

     

     

    Edited to try and clean it up a bit

     

  • your message is mixed up.

    is this what you are doing?

    http://subdomain.domain.com/1050/38745/productdata-reviews/category.htm

    Redirect to http://www.domain.com/productdata/

    http://subdomain.domain.com/1050/38745/productdata-moredata-reviews/categories.htm

    Redirect to http://www.domain.com/productdata-moredata/

    if so, this is an example.

    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
      if { [HTTP::host] equals "subdomain.domain.com" and [HTTP::uri] ends_with "category.htm" } {
        if { [scan [HTTP::path] {/%*d/%*d/%[^-]-%*s} str] == 1 } {
          HTTP::respond 301 noserver Location "http://www.domain.com/${str}/"
        }
      } elseif { [HTTP::host] equals "subdomain.domain.com" and [HTTP::uri] ends_with "categories.htm" } {
        if { [scan [HTTP::path] {/%*d/%*d/%[^-]-%[^-]-%*s} str1 str2] == 2 } {
          HTTP::respond 301 noserver Location "http://www.domain.com/${str1}-${str2}/"
        }
      }
    }
    }
    
    [root@ve10:Active] config  curl -I http://subdomain.domain.com/1050/38745/productdata-reviews/category.htm
    HTTP/1.0 301 Moved Permanently
    Location: http://www.domain.com/productdata/
    Connection: Keep-Alive
    Content-Length: 0
    
    [root@ve10:Active] config  curl -I http://subdomain.domain.com/1050/38745/productdata-moredata-reviews/categories.htm
    HTTP/1.0 301 Moved Permanently
    Location: http://www.domain.com/productdata-moredata/
    Connection: Keep-Alive
    Content-Length: 0
    
    
  • I need to apologize for a few things. I realize I wasn't very clear in my specs and I had a typo at the end of my last comment. Both examples should have contained category.htm at the end. I appreciate the help you have given me and I am going to attempt to adjust what you have given me to fit my needs unless you happen to see this and respond before I make something work. I think I have clarified the specs below and will update when I have a working rule in place.

     

    Here are the three conditions.

     

     

    http://subdomain.domain.com/1050/38745/productdata-reviews/reviews.htm redirected to http://www.domain.com/38745/

     

    In the above example I am only interested in the second set of numbers(38745) in this particular example. The triggering condition needs to be the http::host and the reviews.htm at the end of http::path. The HTTP::host, the 1050 and the reviews.htm at the end of the path are all constant.

     

     

    http://subdomain.domain.com/1050/38745/productdata-reviews/category.htm Redirect to http://www.domain.com/productdata/

     

    http://subdomain.domain.com/1050/38745/productdata-moredata-reviews/category.htm Redirect to http://www.domain.com/productdata-moredata/

     

    In the above example I am only interested in the third section of the http::path. It may contain a random word which I have shown as productdata or a hyphenated random word shown as productdata-moredata. The third section of the http::path will also contain -reviews which is constant and not needed in the redirected url. The triggering condition needs to be the http::host and the category.htm at the end of http::path.

     

  • e.g.

    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
      if { [HTTP::host] equals "subdomain.domain.com" } {
    
        if { [HTTP::path] ends_with "reviews.htm" } {
          if { [scan [HTTP::path] {/%*d/%d/%*s} num] == 1 } {
            HTTP::respond 301 noserver Location "http://www.domain.com/${num}/"
          }
    
        } elseif { [HTTP::path] ends_with "category.htm" } {
          if { [scan [HTTP::path] {/%*d/%*d/%[^/]/%*s} str] == 1 } {
            HTTP::respond 301 noserver Location "http://www.domain.com/[string map {"-reviews" ""} $str]/"
          }
        }
    
      }
    
    }
    }
    
    [root@ve10:Active] config  curl -I http://subdomain.domain.com/1050/38745/productdata-reviews/reviews.htm
    HTTP/1.0 301 Moved Permanently
    Location: http://www.domain.com/38745/
    Connection: Keep-Alive
    Content-Length: 0
    
    [root@ve10:Active] config  curl -I http://subdomain.domain.com/1050/38745/productdata-reviews/category.htm
    HTTP/1.0 301 Moved Permanently
    Location: http://www.domain.com/productdata/
    Connection: Keep-Alive
    Content-Length: 0
    
    [root@ve10:Active] config  curl -I http://subdomain.domain.com/1050/38745/productdata-moredata-reviews/category.htm
    HTTP/1.0 301 Moved Permanently
    Location: http://www.domain.com/productdata-moredata/
    Connection: Keep-Alive
    Content-Length: 0