2

My PHP app is not working because of $_SERVER['REQUEST_URI'] returns the full url to the script instead of a relative path.

My environment:
Windows 7 64 bit.
XAMPP Version 1.8.2
PHP Version 5.4.16
Apache Version Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16

My Virtual Host Conf:

<VirtualHost *:80>
DocumentRoot "D:/HTDOCS/ivankristianto"
ServerName www.ivankristianto.local
UseCanonicalName Off
<Directory "D:/HTDOCS/ivankristianto">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
    Require all granted
</Directory>
</VirtualHost>

I created a basic script to test the $_SERVER content:

echo '$_SERVER[\'HTTP_HOST\'] : ' . $_SERVER['HTTP_HOST'];
echo '<br/>' . '$_SERVER[\'PHP_SELF\'] : ' . $_SERVER['PHP_SELF'];
echo '<br/>' . '$_SERVER[\'REQUEST_URI\'] : ' . $_SERVER['REQUEST_URI'];

And here is the result:

// URL: http://localhost/ivankristianto/request.php
$_SERVER['HTTP_HOST'] : localhost
$_SERVER['PHP_SELF'] : /ivankristianto/request.php
$_SERVER['REQUEST_URI'] : /ivankristianto/request.php //This is correct

And

// URL: http://www.ivankristianto.local/request.php
$_SERVER['HTTP_HOST'] : www.ivankristianto.local
$_SERVER['PHP_SELF'] : /request.php
$_SERVER['REQUEST_URI'] : http://www.ivankristianto.local/request.php  //This is wrong

I didn't use any proxy, all I did is just set it in my /etc/hosts.

I have spent hours to find out why this is happens and have been search through google and this website, but cannot find any clue.

Can you please point me out what's wrong?

Thanks.
Ivan

Burpy Burp
  • 459
  • 3
  • 12
Ivan
  • 714
  • 1
  • 12
  • 28
  • hi, I have the very same issue, but only when accessing the local virtual host on my phone using USB Debug in Chrome – sKopheK Dec 18 '19 at 12:18

3 Answers3

3

I believe you're receiving the desired effect of creating a virtual host:

No Virtual Host:

/ivankristianto/request.php 

With Virtual Host:

http://www.ivankristianto.local/request.php

http://www.ivankristianto.local - I think this seems wrong to you because it contains http://www and .local - you could change this to just invankristano and your REQUEST_URI would output the same as if you had no virtual host. It's representing the path to your request.php - that you've set in the hosts file and is therefore valid part of the URI.

So basically what I'm saying is there's nothing wrong.

If it's causing you problems, then one solution would be to determine which environment you're in - e.g

if($_SERVER['HTTP_HOST'] == 'www.ivankristianto.local') {
    $dev_env = TRUE;
}else {
    $dev_env = FALSE;
}

then somewhere use that:

if($dev_env) {
    $_SERVER['REQUEST_URI'] = str_replace($_SERVER['HTTP_HOST'],'',$_SERVER['REQUEST_URI']);
}

update

try changing host conf to:

<VirtualHost *:80>
DocumentRoot "D:/HTDOCS/ivankristianto"
ServerName ivankristianto.local
UseCanonicalName Off
<Directory "D:/HTDOCS/ivankristianto">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
    Require all granted
</Directory>
</VirtualHost>

(remove www. from ServerName)

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
JohnnyFaldo
  • 4,121
  • 4
  • 19
  • 29
  • 1
    Thanks JohnnyFaldo for your answer. But REQUEST_URI should not return Full URL, it should return to something like this: '/request.php'. Based on PHP Doc: http://php.net/manual/en/reserved.variables.server.php – Ivan Oct 05 '14 at 09:35
  • That's what it is doing. That's my point - you're confused by the 'URL' syntax of your virtual host - if you changed it in host files to just 'ivankristianto' without the www. & .local - then it will seem less strange. It's just a name that refers to the actual part of the URI it represents set in the hosts file – JohnnyFaldo Oct 05 '14 at 09:39
  • @Ivan have you tried getting rid of the UseCanonicalName line in the hosts file – JohnnyFaldo Oct 05 '14 at 09:44
  • yes i did remove it and try again. But still no luck. This is strange and my first time. Request_uri should not return the Full URL. it should return the relative path of the script. – Ivan Oct 05 '14 at 09:47
  • @Ivan change the virtual host to not include wwww - see my updated answer – JohnnyFaldo Oct 05 '14 at 10:27
  • what problem does this cause? – JohnnyFaldo Oct 06 '14 at 08:58
0

I finally got it working.
Here is the steps i did ( i don't know why it is effected, but it is working now ).

  1. Install PHP Fastcgi on xampp, i follow this steps: https://commaster.net/content/installing-php-fastcgi-and-zend-opcache-xampp-windows
  2. I load the mod_fcgid, but i don't use php-cgi.exe handler
  3. Update my /etc/hosts file and flush dns with this command ipconfig /flushdns
  4. Restart apache

And it is working somehow.
Honestly i don't know why it is working, but if someone stumble the same problem, i hope the solution might help.

Ivan
  • 714
  • 1
  • 12
  • 28
0

Had the same issue recently,
My solution:
Firstly check if your http://localhost has the same effect.(which i see i didn't)
If not then added your virtual host(domains) to /etc/host file.

Hope this is helpful.