Forum Discussion

Puneet_73909's avatar
Puneet_73909
Icon for Nimbostratus rankNimbostratus
Mar 03, 2010

HTTP Redirect

Hi,

 

 

I need help in rediret url.

 

 

Whenever user types is http://domain.com/xyz or any uri it should redirect it with 301 redirect.

 

 

I have done with HTTP::respond 301 location and it works.

 

 

But need help in writting an iRule where user types in any URI saying xyz or abc or anything which we don't know then it redirect with 301 response code.

 

 

NEED URGENT HELP!!!!!

 

 

Thanks,

 

Puneet

6 Replies

  • Hi Puneet,

    If you want to redirect all requests made via HTTP to HTTPS with a 301, you can use this:

      
      when HTTP_REQUEST {  
        
          Send 301 redirect to https with host and URI preserved  
         HTTP::respond 301 Location "https://[HTTP::host][HTTP::uri]"  
      }  
      

    If that's not what you're trying to do, can you explain further?

    Thanks,

    Aaron
  • Thanks for your quick reply..

     

     

    given below is my current iRule...

     

     

     

    when HTTP_REQUEST {

     

    if {([string tolower [HTTP::uri]] starts_with "/doc.html")} {

     

    HTTP::redirect "http://[HTTP::host]/doc.html"}

     

    elseif {[string tolower [HTTP::uri]] equals "/sitemap.xml"} {

     

    pool new}

     

    }

     

     

    Now My requirement is to add another statement where I don't know the value of URI and need whenever user types in any URI it should redirect it with 301 redirect.

     

     

     

    Thanks,

     

    Puneet

     

  • Hi Puneet,

    I'm not certain I understand what you want to redirect to in the else case, but you can customize it as necessary:

      
      when HTTP_REQUEST {  
        
          Check the requested URI set to lowercase  
         switch -glob [string tolower [HTTP::uri]] { 
            "/doc.html*" {  
                URI starts with /doc.html  
               HTTP::redirect "http://[HTTP::host]/doc.html"  
            } 
            "/sitemap.xml" {  
                URI is exactly /sitemap.xml  
               pool new  
            } 
            default {  
                No case matched, using default 301 redirect  
               HTTP::respond 301 Location "http://newurl"  
            }  
         }  
      }   
      

    Aaron
  • Thanks!!!

     

     

     

    You mean to say when nothing is matching it will go to "default" and there I will add given below statement.

     

     

     

    HTTP::respond 301 Location "http://[HTTP::host][HTTP::uri]"

     

     

    What this iRules says that whenever user types in /doc it will go to concern defined iRule and when it will site.xml it will go to new pool. But when nothing matches it will go to default and redirect the path (HOST+URI) as 301 redirect..

     

     

    Correct me If I am wrong.

     

     

    Thanks,

     

    Puneet Khanna

     

     

  • That's pretty much it. A user would have to type in /doc.html followed by anything in order to get redirected for the first case.

     

     

    Aaron
  • Puneet,

     

     

    Aaron is correct, but i just want to add.

     

    HTTP::redirect sends the response to the client immediately. Therefore, you cannot specify this command multiple times in an iRule. This command uses a 302 response code.

     

     

    The "default" section in "switch" is similar to the last "else" in "if elseif else" condition.

     

    If nothing else is match above then it will go to this section.

     

     

    Hope it helps!