php/js 重定向
php/js 重定向

需要在iPhone访问的时候把用户重定向到适合iPhone浏览的版本。有JavaScript重定向和PHP重定向两种方法。

// JavaScript 重定向
 if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
 window.location = "https://www.1pxeye.com/iphone";
 }
// PHP 重定向
 if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod')){
 header('Location: https://www.1pxeye.com/iphone');
 exit();
 }

上面两段代码的作用都是在 iPhone 访问的时候重定向到 http://codecto.com/iphone 原理都是通过浏览器的 User Agent 来进行判断,不同之处在于使用 JS 重定向的话只能通过页面跳转,而 PHP 重定向则可以根据需要进行 301 或者 302 跳转。