Rich-text Reply

Redirects when the site supports HTTP and HTTPS

00gauge 06-18-14
Accepted Solution

Redirects when the site supports HTTP and HTTPS

[ Edited ]

I have a site that uses both HTTP and HTTPS and users often land on HTTP and eventually end up on HTTPS. I'd like to set up a test that would require a redirect and it's important that if the user was on HTTP that he gets redirected to a page HTTP and if he was on HTTPS then he'd get redirected to a page on HTTPS. Optimizely redirect option seems to require a protocol (e.g. How would I do this?

Level 1

Ricky 06-19-14

Re: Redirects when the site supports HTTP and HTTPS

[ Edited ]

Great question. While the default functionality will hardcode the protocol ( http: or https: ), it is pretty easy to make a small change to pull the value directly from the page the user is currently on and construct the redirect URL from that.


For example, the default redirect code looks something like this when you set up a redirect with live query parameters and open up the 'Edit Code' section:


Screen Shot 2014-06-18 at 7.06.03 PM.png


You can see the 'http:' hardcoded directly into line 3. However, if you use the window.location.protocol property in JavaScript you can actually determine the protocol of the current page. The new code would look something like this:


 Screen Shot 2014-06-18 at 7.10.03 PM.png


The same thing is true of redirects without the live query parameters, you just need to change line 2 instead of line 3:


Screen Shot 2014-06-18 at 7.11.19 PM.png


Notice that window.location.protocol returns the value with the colon, so you don't need to include that in the hardcoded part of the URL.


You can get most of the variation code by just doing the default redirects, and then just make the small changes outlined here to persist the protocol across pages