---
title: How to Code a PHP Redirect - PHP Header Redirect
description: How to Code a PHP Redirect - PHP Header Redirect
---

[Blog](https://www.codextiny.com/blog)

# [How to Code a PHP Redirect - PHP Header Redirect](https://www.codextiny.com/blog/php-header-redirect)

 Written by [Codextiny](https://www.codextiny.com/blog/author/codextiny) | Dec 23, 2023 4:51:46 PM

Redirecting visitors in PHP is straightforward using the  header()  function. Here's how it works:

1. Create a simple PHP file:  
   Name it  index.php  and place it within the directory you want to initiate the redirect from.
2. Add the following code to the file:
   
   `<?php header("Location: http://www.destination-url.com/"); ?>`  
   Replace  http://www.destination-url.com/  with the actual URL you want users to be redirected to.
   
   You can also specify a file path instead of a URL:
   
   `<?php header("Location: anotherDirectory/anotherFile.php"); ?>`
3. Save the file:  
   When a visitor accesses this  index.php  file, they'll be seamlessly redirected to the specified URL or file.

Key points

- Flexibility: The  header()  function can redirect to various file types, including HTML, Python, PHP, CGI, Perl, and compiled CGI programs.
- Multiple uses: Redirections are valuable for tasks like 
    - Moving visitors to updated pages
    - Forwarding to different sections of your website
    - Handling logins and access control

[View full post](https://www.codextiny.com/blog/php-header-redirect)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Codextiny"
  },
  "dateModified" : "2023-12-23T17:21:19.107Z",
  "datePublished" : "2023-12-23T16:51:46Z",
  "headline" : "How to Code a PHP Redirect - PHP Header Redirect",
  "image" : {
    "@type" : "ImageObject",
    "height" : 720,
    "url" : "https://22535539.fs1.hubspotusercontent-na1.net/hubfs/22535539/Imported%20sitepage%20images/5796.png",
    "width" : 1280
  },
  "mainEntityOfPage" : "https://www.codextiny.com/blog/php-header-redirect",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60,
      "url" : "/hs/hsstatic/content_shared_assets/static-1.4092/img/default-amp-logo.png",
      "width" : 60
    },
    "name" : "Blog"
  }
}
```