'How to use php header() to redirect to homepage?

I have a link in my dashboad page(http://localhost/dashboard.php). The link is as following:

<a class="nav-link" href="logout.php">
  <img class="icon mb-n1px" src="assets/icons/sign_out.svg" alt="category icon"> 
  Log Out
</a>

In the logout.php file, I have the following code:

<?php   
    session_unset();
    header("Location: /"); 
  exit();
?>

So clicking on that link the user agent is redirected to http://localhost/. I want to redirect to http://localhost without the trailing slash. My question is

  1. Why does header("Location: "); not work?
  2. Why does header("Location: ../"); still lead to http://localhost/.
  3. Why does header("Location: /../"); doesn't remove the trailing slash?

P.S: I have read the official docs https://www.php.net/manual/en/function.header.php and other SO posts but didn't find any explanation.



Solution 1:[1]

You can use absolute url header("Location: http://localhost");

  1. Because you need to specify where do you want to redirect
  2. Because http://localhost and http://localhost/ are equal
  3. Because there is no upper level then /

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1