Search code examples
htmlweb-frontendhttp-equiv

HTML Redirect Dependent on Device Type


I have a very simple webpage:

<html>
<head>
<meta http-equiv="Refresh" content="0; url='https://www.website1.com'" />
</head>
<body>
</body>
</html>

Instead of this always redirecting to the same website, I would like to redirect to either website1 or website2 depending on if the user is on a mobile device or a browser on their computer. I am new to front-end web development, so any help you could provide would be much appreciated.

Thanks!


Solution

  • You should use Javascript for that. Check for device type based on the userAgent, and then redirect to the new website

    <script>
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
     // redirects if mobile
     window.location('https://example.com')
    } else {
      // redirects if whatever else
      window.location('https://example2.com')
    }
    </script>