Forum Discussion

1000blocks's avatar
1000blocks
Icon for Nimbostratus rankNimbostratus
Jul 07, 2016

301 iRule redirection generating an extra 301 status code?!?

Hi,

 

We have this iRule in place for 301 redirection...

 

when HTTP_REQUEST {

 

if { [string tolower [HTTP::host]] eq "sitename.com" } then {

 

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

 

} else {

 

HTTP::respond 301 Location "https://[getfield [HTTP::host] ":" 1][HTTP::uri]"

 

}

 

}

 

 

The rule works - but the only thing is that when check the website, we're seeing two 301 redirections happening.

 

https://sitename.com/

 

301 Moved Permanently

 

http://www.sitename.com/

 

301 Moved Permanently

 

https://www.sitename.com/

 

200 OK

 

 

Ideally we want to get it down to showing as one single 301 redirect.

 

Does anyone have any ideas on where the additional 301 redirect could be coming from?

 

6 Replies

  • If I had to hazard a guess I'd say that you have two Virtual servers at play here. Based on the iRule posted I would expect https://sitename.com to redirect to https://www.sitename.com, so that I can't explain. I am guessing that you have another virtual server listening for connections to www.sitename.com:80 and redirecting to :443, which accounts for the second. My recommendation would be to add some logging to your iRule and take an httpwatch capture while it's connecting. You can then compare the logs on the bigIP with the httpwatch and see what is actually happening.
  • Hi Yann. Thanks for your help. Answers to your questions below...

     

     

    Yes - sitename.com and www.sitename.com resolve to the same Virtual Server IP address.

     

     

    No - it's not a typo! If we check the HTTP redirection actions that are happening in the browser when accessing our site, it is like this below...

     

     

    https://sitename.com/

     

    301 Moved Permanently

     

    http://www.sitename.com/

     

    301 Moved Permanently

     

    https://www.sitename.com/

     

    200 OK

     

    Strange eh?!!

     

  • Hi,

    May I suggest to use the irule below instead :

    when HTTP_REQUEST { 
        if { [string tolower [HTTP::host]] eq "sitename.com" } { 
            HTTP::respond 301 Location "https://www.sitename.com[HTTP::uri]" Test TestRedirect Connection Close
        }
    }
    

    I think that the irule originally posted may generate a loop on the client side. moreover, you should see TCL errors on ltm log files as then keyword is not supported (AFAIK)

    Do you have a backend that is listening on http ? Can you check that this is really the F5 that generate the redirect not the backend ?

    In my example, a redirect caused by F5 should generate a specific header named Test to be sent with the redirect response.

  • Can you add logging information to the irule and check in ltm logs (/var/log/ltm in cli or System >> Logs >> Local Traffic in GUI) :

    when HTTP_REQUEST { 
        if { [string tolower [HTTP::host]] eq "sitename.com" } { 
            log local0. "vs_name=[virtual], 301 redirect to https://www.sitename.com[HTTP::uri]"
            HTTP::respond 301 Location "https://www.sitename.com[HTTP::uri]" Test TestRedirect Connection Close
        }
    }