# HTTP Redirects

**URL:** <https://community.f5.com/t/http-redirects/45497>\
**Category:** Technical Forums\
**Tags:** dev, irules, devops, application-delivery\
**Created:** [June 25, 2010, 6:44pm UTC](https://community.f5.com/t/http-redirects/45497 "2010-06-25T18:44:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Frankenfie](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@Eric\_Frankenfie](https://community.f5.com/u/Eric_Frankenfie)\
**Post date:** [June 25, 2010, 6:44pm UTC](https://community.f5.com/t/http-redirects/45497/1 "2010-06-25T18:44:06Z")

</div>

I am new to iRules and going through baptism by fire. I would like the following URLs: [http://www.qa.ipcws.fiserv.com](http://www.qa.ipcws.fiserv.com), [http://qa.ipcws.fiserv.com](http://qa.ipcws.fiserv.com), [https://www.qa.ipcws.fiserv.com](https://www.qa.ipcws.fiserv.com), [https://qa.ipcws.fiserv.com](https://qa.ipcws.fiserv.com) to be redirected to [Login - Client Workstation](https://qa.ipcws.fiserv.com/cws.web/Login.aspx) Any help would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![Eric\_Frankenfie](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@Eric\_Frankenfie](https://community.f5.com/u/Eric_Frankenfie)\
**Post date:** [June 25, 2010, 8:26pm UTC](https://community.f5.com/t/http-redirects/45497/2 "2010-06-25T20:26:04Z")

</div>

This is what I came up with so far…

when HTTP\_REQUEST {

if {[HTTP::host] matches “[https://qa.ipcws.fiserv.com](https://qa.ipcws.fiserv.com)”}

HTTP::redirect “[https://qa.ipcws.fiserv.com/cws.web/Login.aspx](https://qa.ipcws.fiserv.com/cws.web/Login.aspx)”

}

But I receive this error when attempting to save the iRule

01070151:3: Rule [qa.ipcws.fiserv.com-redirect] error:

line 2: [parse error: PARSE syntax 37 {syntax error in expression “[HTTP::host] matches “[https://qa.ipcws.fiserv.com](https://qa.ipcws.fiserv.com)””: extra tokens at end of expression}] [{[HTTP::host] matches “[https://qa.ipcws.fiserv.com](https://qa.ipcws.fiserv.com)”}]

line 2: [missing a script after “if”] []

---

<div class="post-metadata">

**Author:** ![Eric\_Frankenfie](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@Eric\_Frankenfie](https://community.f5.com/u/Eric_Frankenfie)\
**Post date:** [June 25, 2010, 9:33pm UTC](https://community.f5.com/t/http-redirects/45497/3 "2010-06-25T21:33:01Z")

</div>

I believe this did the trick for my VS listening on 443 when HTTP\_REQUEST { if { [string tolower [HTTP::host]] equals “[www.qa.ipcws.fiserv.com](http://www.qa.ipcws.fiserv.com)” } { HTTP::redirect “[Login - Client Workstation](https://qa.ipcws.fiserv.com/cws.web/Login.aspx)” } if { [string tolower [HTTP::host]] equals “[qa.ipcws.fiserv.com](http://qa.ipcws.fiserv.com)” } { HTTP::redirect “[Login - Client Workstation](https://qa.ipcws.fiserv.com/cws.web/Login.aspx)” } } I then created a second VS listening on port 80 and configured this iRule when HTTP\_REQUEST { if { [TCP::local\_port] == 80 }{ HTTP::redirect “https://[getfield [HTTP::host] “:” 1][HTTP::uri]” } }

---

<div class="post-metadata">

**Author:** ![Eric\_Frankenfie](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@Eric\_Frankenfie](https://community.f5.com/u/Eric_Frankenfie)\
**Post date:** [June 25, 2010, 10:38pm UTC](https://community.f5.com/t/http-redirects/45497/4 "2010-06-25T22:38:18Z")

</div>

Upon further testing this does not work…Any help would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [June 26, 2010, 9:27pm UTC](https://community.f5.com/t/http-redirects/45497/5 "2010-06-26T21:27:28Z")

</div>

Hi Eric,

If you want to redirect all of the HTTP requests to HTTPS preserving the URI, you can use the HTTP::redirect rule you posted. You could leave off the check for the port being 80 as all requests to the HTTP VS would be on port 80:

```tcl
HTTP_REQUEST {
   HTTP::redirect "https://[getfield [HTTP::host] ":" 1][HTTP::uri]"
}

```

For the HTTPS VS, you can use an iRule like this to check the Host header value:

```tcl
when HTTP_REQUEST {

    Check the requested host set to lowercase
   switch [string tolower [HTTP::host]] {
      "www.qa.ipcws.fiserv.com" -
      "qa.ipcws.fiserv.com" {
          Matched the host check, send a 301 with the new location
         HTTP::respond 301 Location "https://qa.ipcws.fiserv.com/cws.web/Login.aspx"
      }
      default {
          Take some default action?
      }
   }
}

```

Aaron

---

<div class="post-metadata">

**Author:** ![Eric\_Frankenfie](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@Eric\_Frankenfie](https://community.f5.com/u/Eric_Frankenfie)\
**Post date:** [June 27, 2010, 12:06am UTC](https://community.f5.com/t/http-redirects/45497/6 "2010-06-27T00:06:58Z")

</div>

I removed the iRule from the HTTPS VS deleted the iRule, recreated the iRule with a different, and applied the iRule to the VS. However, the redirect is not working when I navigate to [https://www.qa.ipcws.fiserv.com/](https://www.qa.ipcws.fiserv.com/). I am I missing any thing?

---

<div class="post-metadata">

**Author:** ![Eric\_Frankenfie](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@Eric\_Frankenfie](https://community.f5.com/u/Eric_Frankenfie)\
**Post date:** [June 27, 2010, 12:12am UTC](https://community.f5.com/t/http-redirects/45497/7 "2010-06-27T00:12:00Z")

</div>

Thanks! I just tested the HTTP to HTTPS redirect and that is functioning…Still trying to get the redirect from [www.qa.ipcws.fiserv.com](http://www.qa.ipcws.fiserv.com) to [qa.ipcws.fiserv.com](http://qa.ipcws.fiserv.com) to work.

---

<div class="post-metadata">

**Author:** ![Eric\_Frankenfie](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@Eric\_Frankenfie](https://community.f5.com/u/Eric_Frankenfie)\
**Post date:** [June 27, 2010, 12:30am UTC](https://community.f5.com/t/http-redirects/45497/8 "2010-06-27T00:30:55Z")

</div>

I just captured the traffic in Wireshark to determine if the Redirect was occurring. I did not see the redirect, but it could be due to the encrypted packets.

---

<div class="post-metadata">

**Author:** ![Eric\_Frankenfie](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@Eric\_Frankenfie](https://community.f5.com/u/Eric_Frankenfie)\
**Post date:** [June 27, 2010, 12:46am UTC](https://community.f5.com/t/http-redirects/45497/9 "2010-06-27T00:46:57Z")

</div>

I am currently running version 9.4.8. I check to see if there were any known issues, but I could not find any.

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [June 28, 2010, 1:19pm UTC](https://community.f5.com/t/http-redirects/45497/10 "2010-06-28T13:19:42Z")

</div>

Can you try using a browser plugin to view the unencrypted HTTP requests and responses? You can use HttpFox for Firefox or Fiddler for IE.

I think you’ll find that you’re getting a redirect loop because requests to any URI for [https://qa.ipcws.fiserv.com](https://qa.ipcws.fiserv.com) are being redirected to [https://qa.ipcws.fiserv.com/cws.web/Login.aspx](https://qa.ipcws.fiserv.com/cws.web/Login.aspx). One way to fix this is by redirecting all requests for [www.qa.ipcws.fiserv.com](http://www.qa.ipcws.fiserv.com) to [qa.ipcws.fiserv.com](http://qa.ipcws.fiserv.com) and only requests for [qa.ipcws.fiserv.com](http://qa.ipcws.fiserv.com) and / to the login page:

```tcl
when HTTP_REQUEST {

   log local0. "[IP::client_addr]:[TCP::client_port]: [HTTP::method] request to [HTTP::host][HTTP::uri]"

    Check the requested host set to lowercase
   switch [string tolower [HTTP::host]] {
      "www.qa.ipcws.fiserv.com" {
          Matched the host check, send a 301 with the new location
         log local0. "[IP::client_addr]:[TCP::client_port]: Matched www.qa.ipcws.fiserv.com, redirecting to login page"
         HTTP::respond 301 Location "https://qa.ipcws.fiserv.com/cws.web/Login.aspx"
      }
      "qa.ipcws.fiserv.com" {
          Matched the host check, send a 301 with the new location for requests to /
 if {[HTTP::uri] eq "/"}{
            log local0. "[IP::client_addr]:[TCP::client_port]: Matched qa.ipcws.fiserv.com and /, redirecting to login page"
            HTTP::respond 301 Location "https://qa.ipcws.fiserv.com/cws.web/Login.aspx"
 }
      }
      default {
          Take some default action?
         log local0. "[IP::client_addr]:[TCP::client_port]: No match, doing nothing"
      }
   }
}

```

Aaron

---

<div class="post-metadata">

**Author:** ![Eric\_Frankenfie](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@Eric\_Frankenfie](https://community.f5.com/u/Eric_Frankenfie)\
**Post date:** [June 28, 2010, 1:34pm UTC](https://community.f5.com/t/http-redirects/45497/11 "2010-06-28T13:34:30Z")

</div>

So, it appears that the redirect is happening…but it is happening AFTER the SSL handshake. So users are presented the SSL certificate error and then if you click continue, the redirect occurs and the correct page is displayed. Is there a way to get the redirect to occur before the SSL handshake so the users are not presented with the SSL name mismatch?

---

<div class="post-metadata">

**Author:** ![Michael\_Yates](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@Michael\_Yates](https://community.f5.com/u/Michael_Yates)\
**Post date:** [June 28, 2010, 2:33pm UTC](https://community.f5.com/t/http-redirects/45497/12 "2010-06-28T14:33:14Z")

</div>

Unfortunately, no there is no way to prevent this for two reasons.

1. You can’t modify encrypted traffic until it is decrypted, which won’t happen due to the SSL Mismatch (until you override it).

2. The iRule Processing Event Order has several events prior to HTTP\_REQUEST event, which is what you are using.

RULE\_INIT

CLIENT\_ACCEPTED

CLIENTSSL\_HANDSHAKE

CLIENTSSL\_CLIENTCERT

HTTP\_REQUEST

The best you are going to be able to do in this case is apply the Redirect iRule to the HTTPS Virtual Server and let the user be redirected after the SSL Certificate Mismatch Error.

Another way to do it would be to aim your @ record to a redirect Virtual Server that would do the “add www. and redirect to the www Virtual Server”, but you would have to get an SSL Certificate to apply to that Virtual Server, for your URL (without www) to prevent an SSL Certificate Error if the user access the HTTPS side of the URL.
