임시 리디렉션 (상태코드 : 302)

이 방식은 흔히 사용되는 방법입니다.

<?php 
 function Location_redirect($url)  
 
    if (!headers_sent()) 
    { 
        header('Location: http://' . $url); 

        header("Connection: close"); 
    } 
    print "<html>\n<body>\n";  
    print "<script>document.location.replace('http://" . $url . "');</script>";  


    print "\n<br />\n<br />\n";  
    print "페이지 이동이 되지 않는 경우 <a href='http://" . $url . "'>여기</a>를 클릭하여 주십시오."; 

    print "\n</body>\n</html>";  

    die();
 }  

 Location_redirect("habonyphp.com");  
 ?>


영구 리디렉션(상태코드: 301)

페이지를 영구적으로 리디렉션 시키는 방법을 말합니다. 이 방법은 .htaccess 로만 구현 가능한 방법이 아니며 유명한 스크립트에서 자주 사용되는 방법입니다.

<?php 
 function Permanent_redirect($url) 
 { 
    if (!headers_sent()) 
    { 
        header('HTTP/1.1 301 Moved Permanently'); 
        header('Location: http://' . $url); 

        header("Connection: close"); 
    } 

    print "<html>\n<body>\n"; 
    print "<script>document.location.replace('http://" . $url . "');</script>"; 


    print "\n<br />\n<br />\n"; 
    print "페이지 이동이 되지 않는 경우 <a href='http://" . $url . "'>여기</a>를 클릭하여 주십시오.";
    print "\n</body>\n</html>"; 

    die(); 
 } 

 Permanent_redirect("habonyphp.com"); 
 ?>


조건부 리디렉션

다음은 조건부 리디렉션입니다. 같은 도메인에서 페이지가 변경된 경우 변경된 페이지로 영구히 이동시키는 방법입니다.

<?php 
 function Conditional_redirect($url, $parse)  
 {
    if(strpos($_SERVER['HTTP_REFERER'], $url)!== false) 
    { 
        if (!headers_sent()) 
        { 
            header("HTTP/1.1 301 Moved Permanently"); 
            header("Location: http://" . $url . "/" . $parse); 

            header("Connection: close"); 
        } 
        print "<html>\n<body>\n";  
        print "<script>document.location.replace('http://" . $url . "/" . $parse . "');</script>";  

        print "\n<br />\n<br />\n";  
        print "페이지 이동이 되지 않는 경우 <a href='http://" . $url . "/" . $parse . "'>여기</a>를 클릭하여 주십시오.";
        print "\n</body>\n</html>";  

        die();  
    } 
 }  

 Conditional_redirect("habonyphp.com", "news.php");  
 ?>

0 댓글