PHP로 redirect하기

PHP로 redirect하기 위해 다음과 같이 할 수 있다. Url Redirect 301 vs 302 참조

<?php
        header("Location: http://test.com/test.php");
        exit();
?>

exit()를 넣어줌으로써 현재 Page에서의 PHP실행을 중지하여 다른 헤더가 redirect에 영향을 주는 것을 막을 수 있다.

301 redirect

301 Redirct는 다음과 같이 한다.

<?php
   header("Location: http://test.com/test.php",TRUE,301);
   exit();
?>

상대경로도 가능

if(preg_match("iPhone|Mobile|UP\.Browser|Android|BlackBerry
|Windows CE|Nokia|webOS|Opera Mini|SonyEricsson|opera mobi
|Windows hone|IEMobile|POLARIS",$HTTP_USER_AGENT))
{
  header("location:/mobile",TRUE,301);exit;
}

역링크