Ebenezer Obasi

~ Web Dev & Designer ~

Perfect code to redirect a website to a different page on load
Posted On Saturday, September 27, 2014

Even though I expect codes like this to be very popular among webmasters, I still feel I should share this here with you. It's a simple code to redirect a landing page to another page or website.


<!--REDIRECTING STARTS-->
<link rel="canonical" href="http://your-website.com/"/> 
<noscript>
<meta http-equiv="refresh" content="0;URL=http://your-website.com/">
</noscript>
<script type="text/javascript">
    var url = "http://your-website.com/";

    // IE8 and lower fix
    if (navigator.userAgent.match(/MSIE\s(?!9.0)/))
    {
        var referLink = document.createElement("a");
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    }

    // All other browsers
    else { window.location.replace(url); }
</script>
<!--REDIRECTING ENDS-->

Paste this code immediately after the opening <html> tag of your web page and replace http://your-website.com/ with the website address you want the page to redirect to.

Email Newsletter

Like what you read here in this blog post?
Get more like it delivered to your inbox daily.


Contact Form

Name

Email *

Message *