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

**URL:** <https://community.f5.com/t/301-irule-redirection-generating-an-extra-301-status-code/49874>\
**Category:** Technical Forums\
**Tags:** irules, devops\
**Created:** [July 7, 2016, 9:09pm UTC](https://community.f5.com/t/301-irule-redirection-generating-an-extra-301-status-code/49874 "2016-07-07T21:09:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![1000blocks](https://avatars.discourse-cdn.com/v4/letter/1/f475e1/32.png) [@1000blocks](https://community.f5.com/u/1000blocks)\
**Post date:** [July 7, 2016, 9:09pm UTC](https://community.f5.com/t/301-irule-redirection-generating-an-extra-301-status-code/49874/1 "2016-07-07T21:09:23Z")

</div>

Hi,

We have this iRule in place for 301 redirection…

when HTTP\_REQUEST {

if { [string tolower [HTTP::host]] eq “[sitename.com](http://sitename.com)” } then {

HTTP::respond 301 Location “[https://www.sitename.com](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.

> **[גלקום – קניית דומיין ופתרונות אחסון אתרים – Your Online Presence Everything...](https://www.galcomm.co.il/)**

301 Moved Permanently

> **[גלקום – קניית דומיין ופתרונות אחסון אתרים – Your Online Presence Everything...](https://www.galcomm.co.il)**

301 Moved Permanently

> **[גלקום – קניית דומיין ופתרונות אחסון אתרים – Your Online Presence Everything...](https://www.galcomm.co.il)**

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?

---

<div class="post-metadata">

**Author:** ![Chris\_Grant](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@Chris\_Grant](https://community.f5.com/u/Chris_Grant)\
**Post date:** [July 7, 2016, 9:33pm UTC](https://community.f5.com/t/301-irule-redirection-generating-an-extra-301-status-code/49874/2 "2016-07-07T21:33:22Z")

</div>

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](https://sitename.com) to redirect to [https://www.sitename.com](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.

---

<div class="post-metadata">

**Author:** ![Yann\_Desmarest\_1](https://avatars.discourse-cdn.com/v4/letter/y/65b543/32.png) [@Yann\_Desmarest\_1](https://community.f5.com/u/Yann_Desmarest_1)\
**Post date:** [July 7, 2016, 9:52pm UTC](https://community.f5.com/t/301-irule-redirection-generating-an-extra-301-status-code/49874/3 "2016-07-07T21:52:09Z")

</div>

Hi,

This irule should not works as “then” is not supported in irules

are you resolving [sitename.com](http://sitename.com) and [www.sitename.com](http://www.sitename.com) to the same Virtual Server IP address ?

When you say that you get a redirect to [http://www.sitename.com/](http://www.sitename.com/), is it a mistake and we should understand https instead ?

---

<div class="post-metadata">

**Author:** ![1000blocks](https://avatars.discourse-cdn.com/v4/letter/1/f475e1/32.png) [@1000blocks](https://community.f5.com/u/1000blocks)\
**Post date:** [July 7, 2016, 10:00pm UTC](https://community.f5.com/t/301-irule-redirection-generating-an-extra-301-status-code/49874/4 "2016-07-07T22:00:18Z")

</div>

Hi Yann. Thanks for your help. Answers to your questions below…

Yes - [sitename.com](http://sitename.com) and [www.sitename.com](http://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…

> **[גלקום – קניית דומיין ופתרונות אחסון אתרים – Your Online Presence Everything...](https://www.galcomm.co.il/)**

301 Moved Permanently

> **[גלקום – קניית דומיין ופתרונות אחסון אתרים – Your Online Presence Everything...](https://www.galcomm.co.il)**

301 Moved Permanently

> **[גלקום – קניית דומיין ופתרונות אחסון אתרים – Your Online Presence Everything...](https://www.galcomm.co.il)**

200 OK

Strange eh?!!

---

<div class="post-metadata">

**Author:** ![Yann\_Desmarest\_1](https://avatars.discourse-cdn.com/v4/letter/y/65b543/32.png) [@Yann\_Desmarest\_1](https://community.f5.com/u/Yann_Desmarest_1)\
**Post date:** [July 7, 2016, 10:08pm UTC](https://community.f5.com/t/301-irule-redirection-generating-an-extra-301-status-code/49874/5 "2016-07-07T22:08:25Z")

</div>

Hi,

May I suggest to use the irule below instead :

```tcl
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.

---

<div class="post-metadata">

**Author:** ![1000blocks](https://avatars.discourse-cdn.com/v4/letter/1/f475e1/32.png) [@1000blocks](https://community.f5.com/u/1000blocks)\
**Post date:** [July 7, 2016, 10:59pm UTC](https://community.f5.com/t/301-irule-redirection-generating-an-extra-301-status-code/49874/6 "2016-07-07T22:59:41Z")

</div>

Interested. With your test rule in place Yann, the flow is this…

> **[גלקום – קניית דומיין ופתרונות אחסון אתרים – Your Online Presence Everything...](https://www.galcomm.co.il/)**

301 Moved Permanently

[http://www.sitename.com/](http://www.sitename.com/) (note - it resolves to http not https)

200 OK

---

<div class="post-metadata">

**Author:** ![Yann\_Desmarest\_1](https://avatars.discourse-cdn.com/v4/letter/y/65b543/32.png) [@Yann\_Desmarest\_1](https://community.f5.com/u/Yann_Desmarest_1)\
**Post date:** [July 7, 2016, 11:08pm UTC](https://community.f5.com/t/301-irule-redirection-generating-an-extra-301-status-code/49874/7 "2016-07-07T23:08:54Z")

</div>

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) :

```tcl
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
    }
}

```
