1

Script:

https://example.com/docs/index.php

In index.php, I have the following code:

header('Location: page2.php');

However, instead of opening page2.php, the following URL is opened:

https://example.com/docs/index.php/page2.php

If I put an absolute URL, everything works.

  1. Why is this happening?
  2. Is there any workaround so that I don't have to use an absolute URL?
3
  • are using any framework ? Commented Nov 2, 2016 at 12:18
  • are you sure that the script isn't https://example.com/docs/index.php/? btw, the location header require a complete absolute URI Commented Nov 2, 2016 at 12:19
  • header('Location: /docs/page2.php'); Commented Nov 2, 2016 at 12:20

1 Answer 1

1
<?php
/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>

http://php.net/manual/en/function.header.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.