요즘 pc 카톡이나 기타 sns에서 사이트 url를 복붙 입력 할때가 많다.
아무리 둘러 봐도 pc사이트 입력된걸 모바일로 봤을때
모바일 경로 설정에 관련 글이 정리가 잘 안되어 있어 글을 쓴다.
자바 스크립트 버전
모바일로접속시 pc 사이트를 모바일로 경로 이동 시키기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <script type="text/javascript"> var SiteURL = document.location.href; var SitePath = document.location.pathname; // 쿼리값 까지 보내야 될경우 추가 var SiteQuery = document.location.search; //console.log(SiteURL); var mobileKeyWords = new Array( 'iPhone', 'iPod', 'BlackBerry', 'Android', 'Windows CE', 'LG', 'MOT', 'SAMSUNG', 'SonyEricsson'); for (var word in mobileKeyWords){ if (navigator.userAgent.match(mobileKeyWords[word]) != null){ location.href ="/m"SiteURL+SitePath+SiteQuery; break; } } </script> |
php 버전으로
모바일로접속시 pc 사이트를 모바일로 경로 이동 시키기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?php //Check Mobile $user=$_SERVER['HTTP_USER_AGENT']; //현재 페이지의 호스트값 불러오기 $host=$_SERVER['HTTP_HOST']; //현재 페이지의 REQUEST_URI 값 $request=$_SERVER['REQUEST_URI']; //현재 페이지의 query 값 $query=$_SERVER['QUERY_STRING']; $mAgent = array("iPhone","iPod","Android","Blackberry", "Opera Mini", "Windows ce", "Nokia", "sony" ); $chkMobile = false; for($i=0; $i<sizeof($mAgent); $i++){ if(stripos( $_SERVER['HTTP_USER_AGENT'], $mAgent[$i] )){ $chkMobile = true; break; } } // 모바일인경우 if($chkMobile) { header("Location: " . "http://". $_SERVER['HTTP_HOST'] ."/m". $_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']); } else { //PC일 경우 } ?> | cs |