dimanche 5 juin 2016

Redirecting to https from http and to non www from www in javascript (without .htaccess)

I'm trying to make a redirect in javascript from http to https. It works fine but I'm also trying to redirect from www to non www. This is the code that I wrote in main file:

    <script type="text/javascript">
        if(window.location.href.substring(0, 7) == 'http://') {
            //from non https and non www to https and non www
            window.location = 'https:// shrink.gq';
        } else if(window.location.href.substring(7, 3) = 'www') {
            //from non https and www to https and non www
            window.location = 'https:// shrink.gq';
        } else if(window.location.href.substring(8, 3) = 'www') {
            //from https and www to https and non www
            window.location = 'https:// shrink.gq';
        }
    </script>

Window.location.href returns the link that i'm on. It partially works but when I'm on https and www it didn't redirect me to https and non www. For example, when I type http:// shrink.gq in address bar, it redirects me fine but when I'm on https:// www.shrink.gq it did not work.

P.S. in the javascript code it is not any space between http:// and website link.




Aucun commentaire:

Enregistrer un commentaire