简体   繁体   中英

URL is not same as PHP $_SERVER['DOCUMENT_ROOT']

I am using Oracle VM VirtualBox + Vagrant + CentOS 7.0 + PHP.

When i enter the page http://192.168.33.10/public/index.php :

header('Location: '.$_SERVER['DOCUMENT_ROOT'] .'/public/index.php');

This should redirect to public/public/index.php , but I don't know why it redirects to /vagrant/public/public/index.php .

How can I fix it?

It's just wrong! First of all you use header() in a wrong way

correct:

header('Location: URL');

And you use the wrong variable:

$_SERVER['DOCUMENT_ROOT']

returns the absolute local path on the machine.

$_SERVER['SERVER_NAME'] would fit better, but remember addings http/https in front of it

BUT:

You don't need to add the current servername - Just add the path

header('Location: /public/index.php');

This will always redirect to HTTP(S)://YOURSERVER/public/index.php

And you can also use relative paths, just remove the trailing slash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM