HTTP 301

HTTP 응답 상태 코드 301 Moved Permanently는 영구적인 URL 리다이렉션을 위해 사용되며, 즉 응답을 수신하는 URL을 사용하는 현재의 링크나 레코드가 업데이트되어야 함을 의미한다. 이 새 URL은 응답에 포함된 위치 필드에 지정되어야 한다. 301 리다이렉트는 사용자가 HTTP를 HTTPS로 업그레이드하게 만드는 최상의 방법으로 간주된다.[1]

RFC 2616은 다음과 같이 언급한다:[2]

  • 클라이언트가 링크 편집 기능이 있으면 요청 URL에 대한 모든 참조를 업데이트해야 한다.
  • 특별한 표시가 없으면 응답은 캐시가 가능할 것.
  • 요청 메소드가 HEAD가 아닌 경우 엔티티는 새로운 URL로 향하는 하이퍼링크와 함께 크기가 작은 하이퍼텍스트 참고사항을 포함해야 한다.
  • GET이나 HEAD 외의 요청에 대해 301 상태 코드를 수신할 경우 클라이언트는 리다이렉트 전에 사용자에게 물어보아야 한다.

예시

클라이언트 요청:

GET /index.php HTTP/1.1
Host: www.example.org

서버 응답:

HTTP/1.1 301 Moved Permanently
Location: http://www.example.org/index.asp

다음은 .htaccess 파일을 사용하여, 안전하지 않은 URL을 안전한 주소로 www 없이 리다이렉트 처리하는 예이다.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L] 

다음은 CGI.pm을 사용한 예이다:

print redirect("https://example.com/newpage.html");

다음은 PHP 리다이렉트를 사용한 예이다:

<?php
header("Location: https://example.com/newpage.html", true, 301);
exit;

Nginx 구성의 예이다:

location /old/url/ {
    return 301 /new/url/;
}

다음은 Express.js를 사용한 리다이렉트의 예이다:

app.all("/old/url", (req, res) => {
    res.redirect(301, "/new/url");
});

같이 보기

각주

  1. Google. “Secure your site with HTTPS”. 《support.google.com》. Google. 2016년 2월 6일에 확인함. 
  2. Fielding, et al (1999-06). "10.3.2 301 Moved Permanently". RFC 2616, p 61. IETF, June 1999. Retrieved from https://tools.ietf.org/html/rfc2616#section-10.3.2.

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.