Forum Discussion

lguerra07_54491's avatar
lguerra07_54491
Icon for Nimbostratus rankNimbostratus
Mar 09, 2010

HTTP to HTTPS 301 redirect with datagroups

I simply need to redirect from HTTP to HTTPS and get a 301 code. I was able to get this to work with the following if statement:

 

 

--------

 

when HTTP_REQUEST {

 

 

 

if { [HTTP::host] eq "www.domain.com" and [HTTP::uri] ends_with "/uri1/test.html" }{

 

 

HTTP::respond 301 Location "https://www.domain.com/uri1/test.html"

 

}

 

 

 

}-----------

 

 

The big question would be, how can I make this work with a datagroup? this is my datagroup so far:

 

------

 

class 301_redirect {

 

"www.domain.com https://www.domain.com/uri1/test.html"

 

"www.domain.com https://www.domain.com/uri2/test.html"

 

 

}------

 

 

any help is appreciated.

 

 

thanks.

8 Replies

  • Can you provide more examples of the host and URI strings you want to check and the URLs you want to redirect to?

     

     

    Is the host check always going to be for one domain and then just checking if the URI matches a string? Is the URI check always to see if the requested URI ends with the URI string?

     

     

    Thanks,

     

    Aaron
  • Yes, the host check is always going to be for one domain and then just checking if the URI matches.

     

     

    This would be a better example.

     

    ------

     

    class 301_redirect {

     

    "www.domain.com/uri1/test.html https://www.domain.com/uri1/test.html"

     

    "www.domain.com/uri2/test.html https://www.domain.com/uri2/test.html"

     

    "www.domain.com/uri3/test.html https://www.domain.com/uri3/test.html"

     

    "www.domain.com/uri4/test.html https://www.domain.com/uri4/test.html"

     

     

    }------
  • So it looks like you want to send a 301 redirect to https if the host is "www.domain.com" and the URI is listed exactly in the datagroup? If so, you can add the URIs to a string datagroup one per line and then use an iRule to check the datagroup and send a redirect:

    Datagroup:

     
     class my_paths_class { 
        "/uri1/test.html" 
        "/uri2/test.html" 
        "/uri3/test.html" 
        "/uri4/test.html" 
     } 
     

    iRule:

    Note: if the LTM version is lower than 9.4.4, prepend $:: to the datagroup name in the iRule only: $::my_paths_class

     
     when HTTP_REQUEST { 
      
         Check if host set to lowercase is www.domain.com 
        if {[string tolower [HTTP::host]] eq "www.domain.com"}{ 
      
            Check if path is in the datagroup 
           if {[matchclass [HTTP::path] equals my_paths_class]}{ 
      
               Send a 301 redirect to https 
      HTTP::respond 301 Location "https://www.domain.com[HTTP::uri]" 
           } 
        } 
     } 
     

    Aaron
  • I tried it and it gave me a page cannot be displayed error when browsing to www.domain.com or to any URI underneath it.

     

     

    thanks
  • Which LTM version are you running?

     

     

    Do you see any TCL errors in /var/log/ltm?

     

     

    If not, can you use a browser plugin like HttpFox for Firefox or Fiddler for IE to check if the redirect is being sent to the client?

     

     

    Thanks,

     

    Aaron
  • Im using version 9.3.1..

     

     

    I took what you gave me and modified it and now it works, but Fiddler is reporting a 302 redirect instead of a 301. Any ideas?

     

     

    ===================

     

    when HTTP_REQUEST {

     

    set mypath [findclass [HTTP::path] $::my_paths_class]

     

     

    if { [TCP::local_port] == 80 }{

     

     

    if { [HTTP::host] equals "www.domain.com" and [HTTP::path] equals $mypath }{

     

     

    HTTP::respond 301 Location "https://www.domain.com[HTTP::uri]"

     

    }

     

    }

     

     

    }

     

    ==========================
  • Thanks Aaron.. I looked at the log files and found errors about the naming convention of my datagroup... I was using "-" (dashes) in the name and apparently thats a problem. (at least on version 9.3.1).

     

     

    I was getting this error:

     

    ----

     

    TCL error: Rule www.domain.com-301-redirect HTTP_REQUEST - cant read ::domain: no such variable while executing findclass [HTTP::path] $::domain-https-301-redirect

     

    -----

     

     

    After I removed the "-" (dashes) from the datagroup name, the 301 redirect worked perfectly, with your irule and mine.

     

     

    Thanks..
  • Glad you figured it out. You can use hyphens in TCL variables (or datagroup names), but you need reference them with curly braces: ${::domain-https-301-redirect}. It's easier to replace the hyphens with underscores or nothing.

     

     

    Aaron