php $_SERVER['REQUEST_URI'] 返回完整的 URL 而不是脚本的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26200070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:13:42  来源:igfitidea点击:

$_SERVER['REQUEST_URI'] returns full URL instead of path to script

phpapacherequest-uri

提问by Ivan

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

我的 PHP 应用程序无法运行,因为 $_SERVER['REQUEST_URI'] 将完整的 url 返回到脚本而不是相对路径。

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

我的环境:
Windows 7 64 位。
XAMPP 版本 1.8.2
PHP 版本 5.4.16
Apache 版本 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:

我创建了一个基本脚本来测试 $_SERVER 内容:

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.

我没有使用任何代理,我所做的只是在我的 /etc/hosts.conf 中设置它。

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

谢谢。
伊万

采纳答案by Ivan

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
  1. 在 xampp 上安装 PHP Fastcgi,我按照以下步骤操作:https: //commaster.net/content/installing-php-fastcgi-and-zend-opcache-xampp-windows
  2. 我加载了 mod_fcgid,但我不使用 php-cgi.exe 处理程序
  3. 更新我的 /etc/hosts 文件并使用此命令 ipconfig /flushdns 刷新 dns
  4. 重启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.

它正在以某种方式工作。
老实说,我不知道它为什么有效,但是如果有人遇到同样的问题,我希望该解决方案可能会有所帮助。

回答by JohnnyFaldo

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

我相信您正在收到创建虚拟主机的预期效果:

NoVirtual Host:

没有虚拟主机:

/ivankristianto/request.php 

WithVirtual Host:

使用虚拟主机:

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

http://www.ivankristianto.local- I think this seems wrong to you because it contains http://wwwand .local- you could change this to just invankristanoand your REQUEST_URIwould 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.

http://www.ivankristianto.local- 我认为这对您来说似乎是错误的,因为它包含http://www.local- 您可以将其更改为 justinvankristano并且您REQUEST_URI将输出与没有虚拟主机一样的输出。它代表您的 request.php 的路径 - 您已在主机文件中设置,因此是 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)

(从 ServerName 中删除 www.)

回答by Letlhogonolo Letlape

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

最近有同样的问题,
我的解决方案:
首先检查您的http://localhost是否具有相同的效果。(我看到我没有)
如果没有,则将您的虚拟主机(域)添加到 /etc/host 文件中。

Hope this is helpful.

希望这是有帮助的。