Knowledge Base
 Up to 70% off  on  WordPress  hosting for WordPress Websites and Stores!

What is a PHP Header Redirect and How Can You Code One

In this article, we will guide you on how to use PHP's header() function to redirect a website. This function is handy when redirecting users to a different page or website. By the end of this article, you will clearly understand how to implement this function in your PHP code. So, let's get started!

What is PHP

PHP stands for Hypertext Preprocessor. This is a widely-used, open source scripting language designed specifically for web development. It's a powerful tool for creating dynamic and interactive web pages. PHP scripts are executed on the server, which means the processing happens on the web server before the resulting HTML is sent to the user's web browser. This allows developers to create web pages that can change content dynamically, interact with databases, manage session tracking, and even build entire eCommerce websites.

PHP is known for its ease of integration with a wide range of databases, its compatibility across various operating systems, and its community support, which provides a vast array of resources, frameworks, and libraries to streamline the development process. It's an essential technology for backend web development and is used in conjunction with HTML, CSS, and JavaScript to produce complex, highly functional websites.

What Is a PHP Header Redirect

A PHP header redirect is a method used in PHP to send a raw HTTP header to the browser to instruct it to navigate to a different URL. This technique is commonly used to redirect users to a new page after an action has been performed, such as submitting a form or completing a login. The PHP header() function is used for this purpose, and it must be called before any output is sent to the browser (meaning before any HTML tags or echoes). This is because headers need to be sent ahead of the content.

The basic syntax for a PHP header redirect is:

<phpCopy codeheader('Location: url'); ?>

Where url is the destination address you want the browser to navigate to. It's important to ensure there's no output before calling header(), or it will result in an error. To prevent errors, you can use output buffering or carefully structure your code to avoid sending any content before the redirect. Additionally, it's a good practice to follow the header() function with an exit; statement to ensure the script stops executing after the redirect is issued, preventing any further code from running that might inadvertently send output or perform actions you wish to avoid after the redirection.

PHP Redirection

PHP redirection can be achieved by using the header() function. To redirect to another page, create an index.php file in the directory you want to redirect from and use the following code:

<?php header("Location: http://www.redirect.to.url.com/"); ?>

Where 'http://www.redirect.to.url.com/' is the URL you wish the users to be redirected to. This can also be a file, like so:

<?php header("Location: anotherDirectory/anotherFile.php"); ?>

Files can be of any type, including but not limited to HTML, python, PHP, CGI, Perl, and compiled CGI programs.

What Are HTTP Status Response Codes

HTTP status response codes are standard codes provided by web servers in response to a client's request to access a webpage. These codes indicate whether a specific HTTP request has been successfully completed, and if not, they provide insight into what went wrong. The codes are part of the HTTP (Hypertext Transfer Protocol) response header and are categorized into five classes, each defined by the first digit of the code:

  • 1xx (Informational). These codes indicate a provisional response that signals that the request has been received and the process is continuing.
  • 2xx (Success). These codes indicate that the request was successfully received, understood, and accepted. For example, 200 OK is the standard response for successful HTTP requests.
  • 3xx (Redirection). These codes indicate that further action needs to be taken by the client to complete the request. This often involves being redirected to another URL, such as with a 301 Moved Permanently or 302 Found status code.
  • 4xx (Client Error). These codes indicate an error that occurred due to the client's request not being fulfilled. This could be due to a bad request format, unauthorized access attempt, or a request for a non-existent page. A common example is 404 Not Found.
  • 5xx (Server Error). These codes indicate that the server failed to fulfill a valid request. An example is 500 Internal Server Error, which suggests a problem on the web server that prevented it from completing the request.

Best Practices When Setting Up Redirects

When setting up redirects on your website, it's important that you adhere to best practices to maintain a seamless user experience and preserve SEO value. Follow these seven important practices:

  • Prefer 301 redirects for permanent moves. Utilize 301 redirects to indicate that a page has permanently moved. This approach ensures search engines transfer the page's SEO value to the new location.
  • Avoid chains of redirects. Directly redirect to the final destination page to prevent slow load times and possible loss of SEO value. Avoid creating chains where one redirect leads to another.
  • Update internal links. Instead of relying solely on redirects, update your site's internal links to point directly to the new URLs. This enhances both user experience and site performance.
  • Use 302 redirects sparingly. Reserve 302 redirects for situations where a change is only temporary. Unlike 301 redirects, 302s do not pass SEO value to the new page.
  • Regularly monitor and fix redirect issues. Use website crawling tools to regularly check for and address any broken redirects or redirect loops that could badly affect user experience or SEO.
  • Implement redirects at the server level. Whenever possible, manage redirects at the server level rather than through CMS plugins or .htaccess to improve site performance.
  • Be mindful of redirect loops and loops. Ensure your redirects do not create loops, where a page redirects to another page that redirects back to the original page, as this can cause errors and poor user experiences.

Pro Tip: WordPress users can get professional guidance from an expert that understands the ins and outs of WordPress. You can contact our support for assistance.

Related Blog Posts


Our article serves as a practical guide to implementing PHP header redirects. This serves a crucial technique for web developers who are looking to navigate users seamlessly between web pages or to different websites. It begins with an introduction to PHP, a core scripting language for creating dynamic web content, and progresses to detail how the header() function can be used for redirection purposes. It also includes essential insights into HTTP status response codes, which play a pivotal role in web communication by indicating the success or failure of page requests. Furthermore, it offers valuable best practices for setting up redirects, which emphasizes the importance of using them appropriately to enhance user experience and preserve search engine optimization (SEO) value. Through this article, we hope that you've gained a thorough understanding of the technical and strategic aspects of employing PHP redirects in your web development projects.

If you need further assistance, feel free to contact us via Chat or Phone:

  • Chat Support - While on our website, you should see a CHAT bubble in the bottom right-hand corner of the page. Click anywhere on the bubble to begin a chat session.
  • Phone Support -
    • US: 888-401-4678
    • International: +1 801-765-9400

You may also refer to our Knowledge Base articles to help answer common questions and guide you through various setup, configuration, and troubleshooting steps.

Did you find this article helpful?

* Your feedback is too short