根据用户代理如何使用 apache 更改 url 域

According to user agent how to change the url domain using apache

Sample Desktop : www.example.com Tablet : m.example.com Mobile : mobile.example.com

How can i can change to www to m to mobile according to user agent. (in inspect using emulator for mobile and tablet)

Can anyone please give me a solution.

您可以在后端使用过滤器,当用户访问网站时,根据他的用户代理将他重定向到适当的位置。

您还可以从 JavaScript 的角度进行重定向,方法是在用户点击页面时解析用户代理,然后发出如下重定向:

<script type="text/javascript">
  if (navigator.userAgent == <your logic>) {
    window.location = "http://m.example.com";
  } else if (navigator.userAgent == <your logic>){
    window.location = "http://mobile.example.com";
  }
</script>

您可以在网上找到非常好的用户代理解析器,但请注意,解析用户代理并非万无一失,因为 UA 字符串很容易被欺骗。

在Chrome中,您可以安装User-Agent-Switcher插件轻松更改。