-2

I have been browsing a good couple hours regarding php page redirection. It should be quite straight forward task. However, I am unable to understand what is going on...

I have tried the following two lines of code.

header('Location: http://www.google.ca');

It works!

header('Location: localFile.php');

It does not work!

//redirect.php
<?php
    header('Location: localFile.php');
?>

//localFile.php
<?php
    echo "good!";
?>

My public_html directory contains localFile.php and redirect.php. I don't think that my code is wrong!! Hope someone can tell me what is going on...

Vega
  • 27,856
  • 27
  • 95
  • 103
Blwe Head
  • 11
  • 3
  • 4
    *How* does it not work? Do you get an error? If so, what is it? What actually happens? – John Conde Apr 01 '19 at 18:14
  • Try putting a leading forward slash: header('Location: /localFile.php'); – Ultimater Apr 01 '19 at 18:14
  • 1
    Get rid of `//redirect.php`. No output is allowed and there is a newline there. – AbraCadaver Apr 01 '19 at 18:15
  • The browser displays "header('Location: localFile.php');" :-( – Blwe Head Apr 01 '19 at 22:38
  • I also changed the localFile.php content to instead of echo... – Blwe Head Apr 01 '19 at 22:41
  • If the browser is showing your PHP source code, then you're not using an HTTP server that executes PHP. – Phil Apr 02 '19 at 03:26
  • Possible duplicate of [PHP code is not being executed, instead code shows on the page](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) though I'm not totally convinced because you said the remote redirect works – Phil Apr 02 '19 at 03:28

1 Answers1

0

A location in the headers refers to the URL and not to files. If the file you want to redirect to is in the root, add a forward slash:

header('Location: /localFile.php');

Your first attempt with https://google.ca worked because it refers to an actual URL

Mark Hartmann
  • 31
  • 2
  • 7
  • OP says the files are in the same directory so a relative location should work fine – Phil Apr 02 '19 at 03:24