{"id":2104,"date":"2021-07-02T03:33:03","date_gmt":"2021-07-02T03:33:03","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=2104"},"modified":"2025-04-06T08:02:37","modified_gmt":"2025-04-06T08:02:37","slug":"php-redirection","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-redirection\/","title":{"rendered":"PHP Redirection"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PHP <code>header()<\/code> function to redirect the web browser to a different URL.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-php-redirection'>Introduction to PHP redirection <a href=\"#introduction-to-php-redirection\" class=\"anchor\" id=\"introduction-to-php-redirection\" title=\"Anchor for Introduction to PHP redirection\">#<\/a><\/h2>\n\n\n\n<p>The redirection allows you to redirect the web browser to a different URL. Typically, you use the redirection in the following cases:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Change the domain name of a website to another.<\/li>\n<li>Replace the URL of a page with another.<\/li>\n<li>Upgrade the HTTP to HTTPS.<\/li>\n<\/ul>\n\n\n\n<p>For example, suppose you have a page with the following URL:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">https:<span class=\"hljs-comment\">\/\/phptutorial.net\/about.php<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And you want to change it to:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">https:<span class=\"hljs-comment\">\/\/phptutorial.net\/about-us.php<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To do that, you use the header() function in the about.php like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\nheader(<span class=\"hljs-string\">'Location: https:\/\/phptutorial.net\/about-us.php'<\/span>, <span class=\"hljs-keyword\">true<\/span>, <span class=\"hljs-number\">301<\/span>);\n<span class=\"hljs-keyword\">exit<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you navigate to the URL https:\/\/phptutorial.net\/about-us.php, PHP will redirect you to the new URL https:\/\/phptutorial.net\/about-us.php.<\/p>\n\n\n\n<p>The following diagram illustrates how redirection works:<\/p>\n\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"530\" height=\"254\" class=\"wp-image-2105\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/07\/php-redirection.png\" alt=\"PHP Redirection\" srcset=\"https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/07\/php-redirection.png 530w, https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/07\/php-redirection-300x144.png 300w\" sizes=\"auto, (max-width: 530px) 100vw, 530px\" \/><\/figure>\n<\/div>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the web browser requests the URL <code>\/about.php<\/code> from the server.<\/li>\n<li>Second, the web server receives the request to the <code>\/about.php<\/code> page and informs the web browser of the new URL: <code>\/about-us.php<\/code>. The web server also sends 301 HTTP code back.<\/li>\n<li>Third, once the web browser receives the HTTP code 301, it requests the new URL <code>\/about-us.php<\/code><\/li>\n<li>Finally, the server responds with the contents of the new URL <code>\/about-us.php<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-header-function'>PHP header() function <a href=\"#php-header-function\" class=\"anchor\" id=\"php-header-function\" title=\"Anchor for PHP header() function\">#<\/a><\/h2>\n\n\n\n<p>The <code>header()<\/code> function has the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">header(string $header, bool $replace = <span class=\"hljs-keyword\">true<\/span>, int $response_code = <span class=\"hljs-number\">0<\/span>): void<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The header() function sends a raw HTTP header. Therefore, you need to call it before sending any output.<\/p>\n\n\n\n<p>To do it, you use the exit construct after calling the header() function.<\/p>\n\n\n\n<p>The <code>header()<\/code> function has the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$header<\/code> specifies the HTTP header string to send. In the case of redirection, you use the <code>Location<\/code> header string for redirection.<\/li>\n<li><code>$replace<\/code> indicates if the header should replace the previous header.<\/li>\n<li><code>$response_code<\/code> is the HTTP status code to send back to the client.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Redirection allows you to redirect a URL to the new one.<\/li>\n<li>Use the <code>header()<\/code> function to redirect a URL.<\/li>\n<li>Call the <code>header()<\/code> function before any code that sends output to the web browser.<\/li>\n<li>Use the <code>exit<\/code> construct immediately after the <code>header()<\/code> function.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2104\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-redirection\/\"\n\t\t\t\tdata-post-title=\"PHP Redirection\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2104\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-redirection\/\"\n\t\t\t\tdata-post-title=\"PHP Redirection\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to use the PHP header() function to redirect the web browser to a different URL. Introduction to PHP redirection # The redirection allows you to redirect the web browser to a different URL. Typically, you use the redirection in the following cases: Change the domain name of a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":81,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2104","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=2104"}],"version-history":[{"count":4,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2104\/revisions"}],"predecessor-version":[{"id":3147,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2104\/revisions\/3147"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=2104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}