IT TIP

PHP로 301 또는 302 리디렉션

itqueen 2020. 12. 6. 22:30
반응형

PHP로 301 또는 302 리디렉션


웹 사이트 시작 단계에서 다음 코드를 사용하여 사용자에게 유지 관리 페이지를 표시하고 나머지 사이트를 표시하는 것을 고려하고 있습니다.

검색 엔진에 올바른 302 리디렉션 상태를 표시하는 .htaccess방법이 있습니까? 아니면 다른 기반 접근 방식을 찾아야 합니까?

$visitor = $_SERVER['REMOTE_ADDR'];
if (preg_match("/192.168.0.1/",$visitor)) {
    header('Location: http://www.yoursite.com/thank-you.html');
} else {
    header('Location: http://www.yoursite.com/home-page.html');
};

A에 대한 302 Found, 즉 임시 리디렉션을 수행합니다

header('Location: http://www.yoursite.com/home-page.html');
// OR: header('Location: http://www.yoursite.com/home-page.html', true, 302);
exit;

영구적 인 리디렉션이 필요하면 다음 301 Moved Permanently을 수행하십시오.

header('Location: http://www.yoursite.com/home-page.html', true, 301);
exit;

더 많은 정보는 Doc 헤더 함수에 대한 PHP 매뉴얼을 확인하십시오 . 또한 exit;사용할 때 전화하는 것을 잊지 마십시오header('Location: ');

그러나 임시 유지 보수를 수행하고 있다는 점을 고려하면 (검색 엔진이 페이지를 색인화하는 것을 원하지 않음) 503 Service Unavailable맞춤 메시지와 함께 를 반환하는 것이 좋습니다 (즉, 리디렉션이 필요하지 않음).

<?php
header("HTTP/1.1 503 Service Unavailable");
header("Status: 503 Service Unavailable");
header("Retry-After: 3600");
?><!DOCTYPE html>
<html>
<head>
<title>Temporarily Unavailable</title>
<meta name="robots" content="none" />
</head>
<body>
   Your message here.
</body>
</html>

다음 코드는 301 리디렉션을 발행합니다.

header('Location: http://www.example.com/', true, 301);
exit;

PHP 또는 htaccess에서 수행하는 방법이 실제로 중요하지 않다고 생각합니다. 둘 다 같은 일을 할 것입니다.

제가 지적하고 싶은 한 가지는 검색 엔진이이 "유지 관리"단계에서 귀하의 사이트 색인을 시작하기를 원하는지 여부입니다. 그렇지 않은 경우 상태 코드 503( "일시적으로 작동 중지")를 사용할 수 있습니다 . 다음은 htaccess 예입니다.

RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !=503
RewriteCond %{REMOTE_HOST} ^192\.168\.0\.1
ErrorDocument 503 /redirect-folder/index.html
RewriteRule !^s/redirect-folder$ /redirect-folder [L,R=503]

PHP에서 :

header('Location: http://www.yoursite.com/redirect-folder/index.html', true, 503);
exit;

현재 사용중인 PHP 리디렉션 코드에서 리디렉션은 302(기본값)입니다.


어떤 헤더를 받고 있는지 확인 했습니까? 302위와 함께 해야하기 때문 입니다.

매뉴얼에서 : http://php.net/manual/en/function.header.php

두 번째 특수한 경우는 "Location :"헤더입니다. 이 헤더를 브라우저로 다시 보낼뿐만 아니라 201 또는 3xx 상태 코드가 이미 설정되지 않은 경우 브라우저에 REDIRECT (302) 상태 코드를 반환합니다.

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

PHP 문서에서 :

두 번째 특수한 경우는 "Location :"헤더입니다. 이 헤더를 브라우저로 다시 보낼뿐만 아니라 201 또는 3xx 상태 코드가 이미 설정되지 않은 경우 브라우저에 REDIRECT (302) 상태 코드를 반환합니다.

그래서 당신은 이미 옳은 일을하고 있습니다.


이 파일을 .htaccess와 같은 디렉토리에 저장하십시오.

RewriteEngine on
RewriteBase / 

# To show 404 page 
ErrorDocument 404 /404.html

# Permanent redirect
Redirect 301 /util/old.html /util/new.php

# Temporary redirect
Redirect 302 /util/old.html /util/new.php

참고URL : https://stackoverflow.com/questions/9363760/301-or-302-redirection-with-php

반응형