{"id":546,"date":"2014-12-20T02:38:20","date_gmt":"2014-12-19T20:38:20","guid":{"rendered":"http:\/\/promincproductions.com\/blog\/?p=546"},"modified":"2022-12-12T07:47:13","modified_gmt":"2022-12-12T13:47:13","slug":"php-headers-301-redirect-vs-302-redirect","status":"publish","type":"post","link":"https:\/\/promincproductions.com\/blog\/php-headers-301-redirect-vs-302-redirect\/","title":{"rendered":"PHP Headers: 301 redirect vs 302 redirect"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2014\/12\/php-header-301-redirect-vs-302-redirect.jpg\" rel=\"attachment wp-att-547\" data-lasso-id=\"259\" data-rel=\"lightbox-gallery-PBWiCmpq\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" width=\"500\" height=\"258\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2014\/12\/php-header-301-redirect-vs-302-redirect-500x258.jpg\" alt=\"Adding an exit is needed for a 301 redirect to work correctly in PHP\" class=\"wp-image-547\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2014\/12\/php-header-301-redirect-vs-302-redirect-500x258.jpg 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2014\/12\/php-header-301-redirect-vs-302-redirect-150x78.jpg 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2014\/12\/php-header-301-redirect-vs-302-redirect.jpg 600w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption class=\"wp-element-caption\">Call <code>exit<\/code> after using <code>header<\/code> in PHP to ensure 301 redirect turns it into a 302 redirect.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Many are confused on how and when to use a 301 vs 302 redirect.  Additionally, how to set the HTTP status code in PHP can be done easily as you understand how it functions.  This tutorial covers all of these topics.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">301 VS 302 Redirect<\/h2>\n\n\n\n<p>The <code>301<\/code> and <code>302<\/code> HTTP status codes are both used for redirecting from one URL to another.  Knowing when to use one vs the other, particularly for proper SEO, is important.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>301<\/code> Redirect: <em>moved permanently<\/em><\/li>\n\n\n\n<li><code>302<\/code> Redirect: <em>moved temporarily<\/em><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">SEO Usage of 301 VS 302 Redirect<\/h2>\n\n\n\n<p>It&#8217;s important to use the proper redirect HTTP status code to maintain SEO value of pages.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">301 Redirect (HTTP Status Code: 301)<\/h3>\n\n\n\n<p>When a URL moves and is never intended to be moved back to that URL, a <code>301<\/code> redirect is the status code of choice.  This is used when restructuring URLs across the site, etc.  When a <code>301<\/code> redirect is used, <em>most<\/em> of the SEO value is passed from <strong>URL A<\/strong> to <strong>URL B<\/strong>.  I say <em>most<\/em> because each search engine has it&#8217;s own algorithms as to how this value is passed.  If a URL needs to be redirected, this is typically the status code of choice.<\/p>\n\n\n\n<p>When a search engine sees a <code>301<\/code> status code from crawling a URL, it is stored on their end that <strong>URL A<\/strong> should now be considered as moved to <strong>URL B<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">302 Redirect (HTTP Status Code: 302)<\/h3>\n\n\n\n<p>A <code>302<\/code> redirect is less common.  It is used in <em>temporary<\/em> situations.  For example, if a page is being redesigned and for a short amount of time it needs to be served from a different URL while development is done, then use a <code>302<\/code> redirect.  Redirect <strong>URL A<\/strong> to <strong>URL B<\/strong> for a short period of time.  Then remove the <code>302<\/code> redirect and <strong>URL A<\/strong> once again becomes the permanent URL.<\/p>\n\n\n\n<p>Search engines typically don&#8217;t modify SEO values while this is being used.  But don&#8217;t abuse this status code as it could confuse your intentions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Return a 301 status code Redirect using PHP<\/h2>\n\n\n\n<p>Writing a redirect in PHP is done with the <code>header<\/code> function in PHP.  Headers need to be set in PHP before any output is called.  This means any calls to <code>echo<\/code>, <code>print<\/code>, <code>var_dump<\/code>, etc. will throw an error.<\/p>\n\n\n\n<p>Redirect headers are written with these simple lines:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>header(\"HTTP\/1.1 301 Moved Permanently\");\nheader(\"Location: https:\/\/www.example.com\");<\/code><\/pre>\n\n\n\n<p>These two header lines do two things:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Set the HTTP status code and message.  The message should follow the <a href=\"https:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html\" target=\"_blank\" rel=\"nofollow noopener\" data-lasso-id=\"260\">standard HTTP status codes<\/a> as defined by W3C prefixed by <code>HTTP\/1.1 <\/code>.<\/li>\n\n\n\n<li>The <code>Location<\/code> header sets the URL that the page is being redirected to.<\/li>\n<\/ol>\n\n\n\n<p>But, there is one important piece missing here.  Once the headers are all set, execution needs to be terminated by calling <code>exit;<\/code><\/p>\n\n\n\n<p>With the sample above, the specified <code>301<\/code> redirect would actually return a <code>302<\/code> HTTP status code as opposed to the expected <code>301<\/code>.<\/p>\n\n\n\n<p>Here is the complete (simplified) code to ensure a <code>301<\/code> status code is returned.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>header(\"HTTP\/1.1 301 Moved Permanently\");\nheader(\"Location: https:\/\/www.example.com\");\nexit;<\/code><\/pre>\n\n\n\n<p>A quick explanation&nbsp;on this &#8211; when the page is loading, if it passes this script at any time, a the status code is added the header and the location is passed. &nbsp;This tells a search engine that the page has now moved it&#8217;s URL and that they should remember this change on their end.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Return a 302 status code to Redirect using PHP<\/h2>\n\n\n\n<p>With the knowledge of how a <code>301<\/code> redirect is done in PHP, a <code>302<\/code> HTTP status code can also be easily returned.  While above it was identified that omitting the <code>exit;<\/code> call would result in a <code>302<\/code> redirect, do not rely on that as a solution.  Instead properly set the headers.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">header(\"HTTP\/1.1 302 Found\");\nheader(\"Location: https:\/\/www.example.com\");\nexit;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Pro Tip: HTTP Status Code Chrome Extension<\/h2>\n\n\n\n<p>In the browser, it&#8217;s helpful to quickly identify HTTP status codes that a URL has responded with.<\/p>\n\n\n\n<p>The browsers developer tools report the HTTP status codes, but that has to be opened and takes many clicks to get to the information needed.  This makes it slow to check if a 301 or 302 status code was used, if multiple redirects are at play, etc.<\/p>\n\n\n\n<p>There is a Chrome browser extension, <strong>Redirect Path<\/strong>, that shows the status code of the pages URL right at the top of the browser without extra clicks to open more tools.  This is a plugin highly worth using.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"#\" target=\"_blank\" rel=\"noreferrer noopener\">Get the <strong>Redirect Path<\/strong> Chrome Extension<\/a><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Many are confused on how and when to use a 301 vs 302 redirect. Additionally, how to set [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":547,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[13,5],"tags":[],"class_list":["post-546","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-development","category-website-development"],"jetpack_featured_media_url":"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2014\/12\/php-header-301-redirect-vs-302-redirect.jpg","jetpack_shortlink":"https:\/\/wp.me\/p4BbcR-8O","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/comments?post=546"}],"version-history":[{"count":4,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/546\/revisions"}],"predecessor-version":[{"id":2996,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/546\/revisions\/2996"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media\/547"}],"wp:attachment":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media?parent=546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/categories?post=546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/tags?post=546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}