{"id":97,"date":"2019-09-16T16:20:46","date_gmt":"2019-09-16T16:20:46","guid":{"rendered":"https:\/\/tutorialio.com\/?p=97"},"modified":"2025-11-04T10:31:59","modified_gmt":"2025-11-04T10:31:59","slug":"do-a-redirect-to-different-url-before-page-load","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/do-a-redirect-to-different-url-before-page-load\/","title":{"rendered":"How Can I Do a Redirect to Different URL Before Page Load?"},"content":{"rendered":"<p>Let\u2019s say that you want all the visitor on the webpage <code>https:\/\/example.com\/initial.php<\/code> to <code>https:\/\/example.com\/final.php<\/code>. This can be done using several methods that involve PHP, JavaScript and HTML. In this article, you will learn about all three different techniques that can be used to redirect your visitors from one webpage to another. Here are a few variables that we are going to use:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$new_url = 'https:\/\/example.com\/final.php';\n<\/code><\/pre>\n<h2 id=\"using-the-php-header-function-to-redirect-a-url\">Using the PHP header() Function to Redirect a URL<\/h2>\n<p>If you want to redirect <code>initial.php<\/code> to <code>final.php<\/code>, you can put the following code inside the <code>initial.php<\/code> webpage. This code sends a new HTTP location header to the browser.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$new_url = 'https:\/\/example.com\/final.php';\nheader('Location: '.$new_url);\n<\/code><\/pre>\n<p>Here we have use the <a href=\"http:\/\/www.php.net\/manual\/en\/function.header.php\">header() function in PHP<\/a> to make the redirect. The important thing that you should keep in mind is that the above code should be placed before any HTML or text output. Otherwise, you will get an <strong>headers already sent<\/strong> error. You can also use output buffering in order to get rid of the <strong>headers already sent<\/strong> error. The following example shows it in action:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">ob_start();\n$new_url = 'https:\/\/example.com\/final.php';\nheader('Location: '.$new_url);\nob_end_flush();\n<\/code><\/pre>\n<p>The <code>ob_start()<\/code> function should be the first function in your PHP script when you want to perform a redirect using the header() function in PHP. This will prevent the <strong>headers already sent error<\/strong> from occurring.<\/p>\n<p>As a safety measure, you might want to add <code>die()<\/code> or <code>exit()<\/code> directly after the header redirect in order to prevent the rest of the code of the webpage from executing. Sometimes, a crawler or a browser will not respect the Location header and this might <a href=\"http:\/\/thedailywtf.com\/articles\/WellIntentioned-Destruction\">jeopardize the security of your website<\/a>.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$new_url = 'https:\/\/example.com\/final.php';\nheader('Location: '.$new_url);\nexit();\n<\/code><\/pre>\n<p>To be clear, <code>die()<\/code> or <code>exit()<\/code> have nothing to do with the redirection. They are used to prevent the rest of the code on the webpage from executing.<\/p>\n<p>It is also recommended to use absolute URLs while specifying a Location header value. However, relative URLs will also work just fine. You can also use this function to redirect users to external websites or webpages.<\/p>\n<h2 id=\"output-javascript-redirection-code-using-php-echo-function\">Output JavaScript Redirection Code using PHP echo() Function<\/h2>\n<p>This is not a pure PHP based solution but that doesn\u2019t diminishes its effectiveness. You can use the <a href=\"..\/string\/echo-function.php\">PHP <code>echo()<\/code> function<\/a> in order to output the JavaScript code that can handle the redirection for you.<\/p>\n<p>With this solution, you will not have to use output buffering and you can also prevent the <strong>headers already sent<\/strong> error from occurring. If you a little JavaScript with PHP is not a problem, this could be a really clever solution. Here are a few examples that use different JavaScript methods to redirect from the current page to another.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">echo \"&lt;script&gt;self.location='https:\/\/example.com\/final.php';&lt;\/script&gt;\";\necho \"&lt;script&gt;document.location.href='https:\/\/example.com\/final.php';&lt;\/script&gt;\";\necho \"&lt;script&gt;window.location.href='https:\/\/example.com\/final.php';&lt;\/script&gt;\";\necho \"&lt;script&gt;window.location.replace('https:\/\/example.com\/final.php');&lt;\/script&gt;\";\n<\/code><\/pre>\n<p>The only downside with this method is that JavaScript runs client-side and your visitors can disable JavaScript if they want to.<\/p>\n<h2 id=\"using-html-meta-tags-to-redirect-a-webpage\">Using HTML meta Tags to Redirect a Webpage<\/h2>\n<p>You can also use basic HTML to perform a redirect from one webpage to another. This may not look very professional but it works without worrying about a client disabling the JavaScript or the <strong>headers already sent<\/strong> error.<\/p>\n<p class=\"lang-name\">HTML<\/p>\n<pre><code class=\"language-html\">&lt;meta http-equiv=\"Location\" content=\"http:\/\/example.com\/final.php\"&gt;\n&lt;!-- The following line will redirect after the given number of seconds. Zero in our case. --&gt;\n&lt;meta http-equiv=\"refresh\" content=\"0;url=http:\/\/example.com\/final.php\"&gt;<\/code><\/pre>\n<p>You can also use the last line from the previous code block in order to auto-refresh a page every \u201cn\u201d seconds. For example, the following line will automatically refresh the page after every 8 seconds.<\/p>\n<p class=\"lang-name\">HTML<\/p>\n<pre><code class=\"language-html\">&lt;meta http-equiv=\"refresh\" content=\"8\"&gt;<\/code><\/pre>\n<h2 id=\"quick-summary\">Quick Summary<\/h2>\n<p>Let\u2019s recap everything that we have covered in this tutorial.<\/p>\n<ol id=\"summary-points\">\n<li>The easiest way to redirect users to a different page in PHP is to use the <code>header()<\/code> function. It is important to call this function before any HTML output in order to avoid headers already sent error.<\/li>\n<li>Another way to avoid the headers already sent error is to use PHP <code>echo()<\/code> function to output JavaScript code to do the redirect. The problem with this solution is that it will not work if the users have disabled JavaScript.<\/li>\n<li>One final method to redirect users to a different URL is to use HTML meta tags. These tags can also be used to refresh the same webpage after \u2018n\u2019 number of seconds.<\/li>\n<\/ol>\n<p>Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to redirect your users to a different page in PHP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s say that you want all the visitor on the webpage https:\/\/example.com\/initial.php to https:\/\/example.com\/final.php. This can be done using several methods that involve PHP, JavaScript and HTML. In this article, you will learn about all three different techniques that can be used to redirect your visitors from one webpage to another. Here are a few [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","category-how-to-guides"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":1,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":10248,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/97\/revisions\/10248"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}