programing

PHP를 사용한 301 또는 302 리다이렉션

coolbiz 2022. 9. 21. 22:52
반응형

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');
};

의 경우302 Found(즉, 일시적인 리다이렉트 실행:

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

영속적인 리다이렉트가 필요한 경우:301 Moved Permanently, 다음 작업을 수행합니다.

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

자세한 내용은 헤더 함수의 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

반응형