{"id":9042,"date":"2023-12-24T17:56:48","date_gmt":"2023-12-24T14:56:48","guid":{"rendered":"https:\/\/infinitydomainhosting.com\/kb\/?p=9042"},"modified":"2025-09-24T19:10:17","modified_gmt":"2025-09-24T16:10:17","slug":"how-do-i-redirect-a-url-to-another-page-in-php","status":"publish","type":"post","link":"https:\/\/infinitydomainhosting.com\/kb\/how-do-i-redirect-a-url-to-another-page-in-php\/","title":{"rendered":"How Do I Redirect a URL to Another Page in PHP?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/infinitydomainhosting.com\/kb\/how-do-i-redirect-a-url-to-another-page-in-php\/#How_Do_I_Redirect_a_URL_to_Another_Page_in_PHP\" >How Do I Redirect a URL to Another Page in PHP?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/infinitydomainhosting.com\/kb\/how-do-i-redirect-a-url-to-another-page-in-php\/#Redirecting_a_URL_to_Another_Page\" >Redirecting a URL to Another Page<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/infinitydomainhosting.com\/kb\/how-do-i-redirect-a-url-to-another-page-in-php\/#FAQs\" >FAQs<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/infinitydomainhosting.com\/kb\/how-do-i-redirect-a-url-to-another-page-in-php\/#Q_Can_I_redirect_a_user_to_a_different_page_based_on_certain_conditions\" >Q: Can I redirect a user to a different page based on certain conditions?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/infinitydomainhosting.com\/kb\/how-do-i-redirect-a-url-to-another-page-in-php\/#Q_Is_there_a_way_to_redirect_users_after_a_form_submission\" >Q: Is there a way to redirect users after a form submission?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/infinitydomainhosting.com\/kb\/how-do-i-redirect-a-url-to-another-page-in-php\/#Q_What_happens_if_I_use_the_header_function_after_output_has_already_been_sent_to_the_browser\" >Q: What happens if I use the header function after output has already been sent to the browser?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/infinitydomainhosting.com\/kb\/how-do-i-redirect-a-url-to-another-page-in-php\/#Q_Are_there_any_other_HTTP_headers_that_I_can_set_using_the_header_function\" >Q: Are there any other HTTP headers that I can set using the header() function?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/infinitydomainhosting.com\/kb\/how-do-i-redirect-a-url-to-another-page-in-php\/#Q_Is_it_possible_to_redirect_users_to_an_external_website_using_the_header_function\" >Q: Is it possible to redirect users to an external website using the header() function?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"How_Do_I_Redirect_a_URL_to_Another_Page_in_PHP\"><\/span>How Do I Redirect a URL to Another Page in PHP?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In <a href=\"https:\/\/infinitydomainhosting.com\/kb\/understanding-website-design-and-website-design-accessibility-considerations\/\">web development<\/a>, it&#8217;s common to need to redirect users from one page to another. This can occur for <a href=\"https:\/\/infinitydomainhosting.com\/kb\/understanding-website-caching-and-website-performance-optimization\/\">a<\/a> variety of reasons, such as when a user submits a form and you want to display a confirmation page or when you want to redirect a user to a new page after they login.<\/p>\n<p>In PHP, you can accomplish this by using the header() function, which sends a raw HTTP header to the browser.<\/p>\n<p>In this article, we&#8217;ll cover the basics of <a href=\"https:\/\/phoenixnap.com\/kb\/php-redirect\" target=\"_blank\" rel=\"noopener\">redirecting a URL to another page in PHP<\/a> and provide some commonly asked questions with answers.<\/p>\n<p>[lwptoc]<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Redirecting_a_URL_to_Another_Page\"><\/span>Redirecting a URL to Another Page<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To redirect a URL to another page in PHP, you can use the header() function with the location attribute set to the desired URL.<\/p>\n<p>Here&#8217;s an example of how to redirect a user to a new page in PHP:<\/p>\n<p>&#8220;`php<br \/>\n<!--?php &lt;br ?--> header(&#8216;Location: http:\/\/www.example.com\/newpage.php&#8217;);<br \/>\nexit;<br \/>\n?&gt;<br \/>\n&#8220;`<\/p>\n<p>In this example, we use the header() function to set the Location header to the URL of the new page we want to redirect to. We then use the exit statement to terminate the script and prevent any further code execution.<\/p>\n<p>It&#8217;s important to note that the header() function must be called before any output is sent to the browser. This means that you should not have any HTML or whitespace before the header() function is called.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Q_Can_I_redirect_a_user_to_a_different_page_based_on_certain_conditions\"><\/span>Q: Can I redirect a user to a different page based on certain conditions?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A: Yes, you can use conditional statements to redirect users to different pages based on specific conditions. For example, you can use an if statement to check if a user is logged in and redirect them to the dashboard if they are, or to the login page if they are not.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Q_Is_there_a_way_to_redirect_users_after_a_form_submission\"><\/span>Q: Is there a way to redirect users after a form submission?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A: Yes, you can redirect users to a new page after they submit a form by using the header() function in conjunction with the form processing code. Once the form is processed and validated, you can use the header() function to redirect the user to a confirmation page or another relevant page.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Q_What_happens_if_I_use_the_header_function_after_output_has_already_been_sent_to_the_browser\"><\/span>Q: What happens if I use the header function after output has already been sent to the browser?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A: If you attempt to use the header() function after output has already been sent to the browser, you will likely encounter an error such as &#8220;Headers already sent.&#8221; To avoid this, ensure that the header() function is called before any output, including HTML, whitespace, or PHP errors.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Q_Are_there_any_other_HTTP_headers_that_I_can_set_using_the_header_function\"><\/span>Q: Are there any other HTTP headers that I can set using the header() function?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A: Yes, in addition to the Location header, you can also set other HTTP headers using the header() function. Some commonly used headers include Content-Type, Cache-Control, and Expires.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Q_Is_it_possible_to_redirect_users_to_an_external_website_using_the_header_function\"><\/span>Q: Is it possible to redirect users to an external website using the header() function?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A: Yes, you can use the header() function to redirect users to an external website by setting the Location header to the external URL. However, it&#8217;s important to be cautious when redirecting users to external websites, as this can open up security risks such as phishing and cross-site scripting (XSS) attacks.<\/p>\n<p>In conclusion, redirecting a URL to another page in PHP is a common task in web development. By using the header() function with the location attribute set to the desired URL, you can easily redirect users to a new page based on specific conditions or form submissions. Remember to ensure that the header() function is called before any output is sent to the browser to avoid errors.<br \/>\n<!--KB_CAT_BLOCK--><\/p>\n<figure class=\"kb-cat-placeholder\" style=\"margin:1.75rem 0;display:block;\"><img src=\"https:\/\/infinitydomainhosting.com\/kb\/assets\/img\/cat-php-scripts.webp\" alt=\"How Do I Redirect a URL to Another Page in PHP?\" loading=\"lazy\" decoding=\"async\" style=\"max-width:100%;height:auto;display:block;border-radius:12px;box-shadow:0 8px 28px rgba(0,0,0,0.12);\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>How Do I Redirect a URL to Another Page in PHP? In web development, it&#8217;s common to need to redirect users from&hellip;<\/p>\n","protected":false},"author":1,"featured_media":13324,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[3],"tags":[70,1930,621],"class_list":["post-9042","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-scripts","tag-php","tag-url-redirection","tag-web-development"],"_links":{"self":[{"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/posts\/9042","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/comments?post=9042"}],"version-history":[{"count":4,"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/posts\/9042\/revisions"}],"predecessor-version":[{"id":43607,"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/posts\/9042\/revisions\/43607"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/media\/13324"}],"wp:attachment":[{"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/media?parent=9042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/categories?post=9042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/tags?post=9042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}