Friday, October 21, 2011

Redirect all traffic to HTTPS in Yii Framework

There is a lot of tutorials on how to redirect traffic through https for parts of your site, but what about if you want all traffic to be redirected through HTTPS?  This task was easy on apache, but when I moved to IIS 7 I just couldnt find an easy way to do it on the server.  So, a little javascripting and we are on our way.

In your Views/templates open the Main.php file and add this script to the top:

<script type="text/javascript">
//<![CDATA[
    function RedirNonHttps() {
        if (location.href.indexOf("https://") == -1) {
            location.href = location.href.replace("http://", "https://");
        }
    }
    //]]>
</script>
Then on your BODY Tag, Change it like this:

< body onload="RedirNonHttps();" >
There you go, Now all traffic will be redirected to use SSL!

No comments:

Post a Comment