# HTTP to HTTPS redirect on non-standard port

**URL:** <https://community.f5.com/t/http-to-https-redirect-on-non-standard-port/23236>\
**Category:** Technical Forums\
**Tags:** irules, dev, devops, application-delivery\
**Created:** [June 30, 2008, 4:50pm UTC](https://community.f5.com/t/http-to-https-redirect-on-non-standard-port/23236 "2008-06-30T16:50:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_48455](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@Jason\_48455](https://community.f5.com/u/Jason_48455)\
**Post date:** [June 30, 2008, 4:50pm UTC](https://community.f5.com/t/http-to-https-redirect-on-non-standard-port/23236/1 "2008-06-30T16:50:02Z")

</div>

I have a web application that is running on a non-standard port. We need to have SSL on it. I applied the client certificate and https works fine, but if we enter http we get a page cannot be displayed.

Can I write a rule to redirect any http requests to this host on this port to https?

If so, how?

Thank you.

---

<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 30, 2008, 6:11pm UTC](https://community.f5.com/t/http-to-https-redirect-on-non-standard-port/23236/2 "2008-06-30T18:11:30Z")

</div>

Sure… you can use an iRule, or if you’re on 9.4+ you can use an HTTP class to perform the redirect.

SOL7125: Configuring a virtual server to automatically redirect HTTP requests to HTTPS

> **[404 Page not found | BIG-IP Documentation](https://techdocs.f5.com/kb/en-us/solutions/public/7000/100/sol7125.html)**
>
> Usage information and technical documentation for BIG-IP and other related F5 products

Aaron

---

<div class="post-metadata">

**Author:** ![Jason\_48455](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@Jason\_48455](https://community.f5.com/u/Jason_48455)\
**Post date:** [June 30, 2008, 6:59pm UTC](https://community.f5.com/t/http-to-https-redirect-on-non-standard-port/23236/3 "2008-06-30T18:59:26Z")

</div>

Unfortunately that didn’t work. I think I need to explain further what I’m trying to do.

When users go to [http://server.domain.com:8700](http://server.domain.com:8700)

I need them to be automatically redirected to [https://server.domain.com:8700](https://server.domain.com:8700)

It would be redirected to the same virtual server, only over https.

---

<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 30, 2008, 7:38pm UTC](https://community.f5.com/t/http-to-https-redirect-on-non-standard-port/23236/4 "2008-06-30T19:38:42Z")

</div>

If the request can be HTTP or HTTPS on the same port, you can track whether there was a client SSL handshake using the CLIENTSSL\_HANDSHAKE event and only send a redirect for requests which didn’t initiate a n SSL handshake. Because the port stays the same, you can redirect to the same host (including the port) and URI.

```tcl
when CLIENT_ACCEPTED { 
     Set a variable to track whether this is an HTTPS request 
    set https 0 
 } 
 when CLIENTSSL_HANDSHAKE { 
     There was a client side SSL handshake, so update the variable 
    set https 1 
 } 
 when HTTP_REQUEST { 
     If it's not an HTTPS connection, send a redirect 
    if {not ($https)}{ 
       HTTP::redirect https://[HTTP::host][HTTP::uri] 
    } 
 }

```

Aaron

---

<div class="post-metadata">

**Author:** ![Jason\_48455](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@Jason\_48455](https://community.f5.com/u/Jason_48455)\
**Post date:** [June 30, 2008, 8:25pm UTC](https://community.f5.com/t/http-to-https-redirect-on-non-standard-port/23236/5 "2008-06-30T20:25:23Z")

</div>

I tried the new irule and it’s still not working. I’m not getting any error, it just says that page cannot be displayed.

Not sure if it makes a difference, but I have a client SSL profile set on the virtual server.

I don’t see any messages at all related to the irule in any of the logs.

Thanks.

---

<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 30, 2008, 8:29pm UTC](https://community.f5.com/t/http-to-https-redirect-on-non-standard-port/23236/6 "2008-06-30T20:29:27Z")

</div>

Do you have non-SSL enabled on the client SSL profile? This will be required in order to issue a redirect on a non-SSL request.

If it still doesn’t work, can you try this version with logging:

```tcl
when CLIENT_ACCEPTED { 
  
    log local0. "[IP::client_addr]:[TCP::client_port]: new connection to [IP::local_port]:[TCP::local_port]. https 0" 
  
     Set a variable to track whether this is an HTTPS request 
    set https 0 
 } 
 when CLIENTSSL_HANDSHAKE { 
  
    log local0. "[IP::client_addr]:[TCP::client_port]: new connection to [IP::local_port]:[TCP::local_port]. https 1" 
  
     There was a client side SSL handshake, so update the variable 
    set https 1 
 } 
 when HTTP_REQUEST { 
     If it's not an HTTPS connection, send a redirect 
    if {not ($https)}{ 
       log local0. "[IP::client_addr]:[TCP::client_port]: new connection to [IP::local_port]:[TCP::local_port] redirecting http" 
  
       HTTP::redirect https://[HTTP::host][HTTP::uri] 
    } 
 }

```

Aaron

---

<div class="post-metadata">

**Author:** ![Jason\_48455](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@Jason\_48455](https://community.f5.com/u/Jason_48455)\
**Post date:** [June 30, 2008, 8:35pm UTC](https://community.f5.com/t/http-to-https-redirect-on-non-standard-port/23236/7 "2008-06-30T20:35:38Z")

</div>

You are awesome. I didn’t have the non-SSL option set.

It’s working now.

Thank you very much!!!

---

<div class="post-metadata">

**Author:** ![norman\_lee](https://avatars.discourse-cdn.com/v4/letter/n/50afbb/32.png) [@norman\_lee](https://community.f5.com/u/norman_lee)\
**Post date:** [February 12, 2010, 10:01pm UTC](https://community.f5.com/t/http-to-https-redirect-on-non-standard-port/23236/8 "2010-02-12T22:01:02Z")

</div>

rule worked wonderfully! Thanks

---

<div class="post-metadata">

**Author:** ![TejasTh\_307036](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@TejasTh\_307036](https://community.f5.com/u/TejasTh_307036)\
**Post date:** [January 25, 2017, 2:49pm UTC](https://community.f5.com/t/http-to-https-redirect-on-non-standard-port/23236/9 "2017-01-25T14:49:46Z")

</div>

I have the same requirement, if user access the URL with HTTPS, no change in URL. If user access the URLwith HTTP, should redirect to https, I am also using non-standard port URLS.

[https://abc.com:8888](https://abc.com:8888) ==\> no change

[http://abc.com:8888](http://abc.com:8888) ==\> redicet to [https://abc.com:8888](https://abc.com:8888)

It’s perfectly working fine with below config

1) Create SSL Client profile with allowing Non-SSL Connections ( You need to modify default config)

2) Create VIP with 8888 port and use SSL client profile

3) Configure below iRule

when HTTP\_REQUEST {

if { [URI::protocol [HTTP::uri]] eq “http” } {

HTTP::redirect https://[getfield [HTTP::host] “:” 1]:[TCP::local\_port][HTTP::uri]

}

}

---

<div class="post-metadata">

**Author:** ![stan\_piron](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@stan\_piron](https://community.f5.com/u/stan_piron)\
**Post date:** [January 25, 2017, 5:10pm UTC](https://community.f5.com/t/http-to-https-redirect-on-non-standard-port/23236/10 "2017-01-25T17:10:53Z")

</div>

Hi,

how do you get protocol from

```tcl
[HTTP::uri]

```

?

when a user request [http://abc.com:8888/def.txt](http://abc.com:8888/def.txt),

```tcl
[HTTP::uri]

```

return only /def.txt

the solution provided by hoolio is the solution.
