Forum Discussion

gr0x_33657's avatar
gr0x_33657
Icon for Nimbostratus rankNimbostratus
Nov 03, 2009

URI Redirect

Hi,

 

 

We would like to redirect all traffic to our maintenance page, so far if someone calls the domain name for example "www.domainname.com" the irule works and redirects the person to "www.domainname.com/maintenance.html" If someone calls "www.domainname.com/sitefolder/" the irule will not work. I tried changing it but the irule will just loop.

 

 

when HTTP_REQUEST {

 

if { [HTTP::path] equals "/" } {

 

HTTP::redirect "/maintenance.html"

 

 

 

Thanks
  • This is because the you have [HTTP::path] equal "/" which means that it will only redirect when there is no URI in the domain.

    For example this will redirect to maintenance if it matches 2 conditions

     
     when HTTP_REQUEST { 
        if { ([HTTP::host] eq "www.domainname.com") and ([HTTP::path] eq "/" } { 
        HTTP::redirect "http://[HTTP::host]/maintenance.html" 
        } 
     } 
     

    If you want it to match on "/sitefolder" and "/"then you can do the following

     
     when HTTP_REQUEST { 
        if {[HTTP::host] eq "www.domainname.com") } { 
        switch -glob [HTTP::path] { 
        "/" { HTTP::redirect "/maintenance.html" } 
        "/sitefolder" { HTTP::redirect "/maintenance.html" } 
        } 
        } 
       } 
     

    You can keep expanding to other URIs as well

    I hope this helps

    CB

  • Thanks, your rules work but is it possible to put a wildcard for HTTP::path, we dont want users to surf the website.
  • or you can do this

     
      
     when HTTP_REQUEST {  
         if {[HTTP::host] eq "www.domainname.com") } {  
         switch -glob [HTTP::path] {  
         "/" { HTTP::redirect "/maintenance.html" }  
         "/sitefolder" { HTTP::redirect "/maintenance.html" }  
                             default   { HTTP::redirect "/maintenance.html" }  
         }  
         }  
        }  
     

    Basically if doesn't match any of the paths select then the default will always match.

    CB

  • I get a redirection loop error in Firefox. I also tried "/*" but that does not work for me.

     

     

     

    Thanks
  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    It would be easier to handle if the maintenance page was in a unique directory. You could then check if the requested path started with that directory before redirecting them. This would avoid a redirect loop:

     
     when HTTP_REQUEST { 
      
         Check if URI does not start with "/maintenance" 
        if {not ([HTTP::path] starts_with  "/maintenance")}{ 
      
           HTTP::redirect  "/maintenance/maintenance.html" 
        } 
     } 
     

    As log as any files the /maintenance/maintenance.html page references are under the /maintenance/ directory, this should work.

    Aaron