有条件重定向到移动子域

问题描述 投票:0回答:2

我创建了一个网站,https://www.example.com。现在,我还创建了一个移动子域,将我最初创建的四个页面合并为一个长卷轴。当我手动转到

m.example.com
时,效果很好,但我之前还打印了一些带有引用主域的二维码的卡片。由于二维码更有可能从移动设备扫描,我想在主页添加一些代码,自动将任何“移动设备”重定向到
m.example.com
,但是当我尝试在 JS 中实现这一点时,我的重定向最终结束请转而将我发送至
example.com/m.example.com
。如何重定向到实际的子域?

到目前为止我的代码非常简单:

//Redirect to mobile subdomain if on mobile device
if (screen.width <= 699) {
    window.location = "m.example.com";
}

我也尝试过

document.location
并且我尝试过基于文档树进行引用 - 尝试指向./mobile/index.php,其中根文件夹是主主页的index.php所在的位置,所以我在哪里我想我会重定向自。请注意,我的JS位于./inc目录中,但是无论我是否包含./(甚至是././),计算机都会说不:( 我也尝试过查看 .htaccess,这对于引用来说似乎很好,但我也没有看到任何有关如何在那里进行条件引用的信息。我使用 PHP 和 JS,当然还有 HTML 和 CSS,所以使用这些语言的任何解决方案都是首选,但我会采取任何有效的方法。

javascript redirect conditional-statements subdomain
2个回答
0
投票

以下是如何实现它的实现:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My App</title>
  <script>
    function isMobileMedia() {
        console.log(screen.width)
        return (screen.width <= 699);
    }

    if (isMobileMedia()) {
        window.location.href = "http://m.example.com";
    }
  </script>
</head>
<body>
  <h1>Welcome to example.com</h1>
</body>
</html>

0
投票

您需要包含

https://
协议:

window.location = 'https://m.example.com/';
© www.soinside.com 2019 - 2024. All rights reserved.