【问题标题】:Codeigniter REQUEST_URI gets wrong value when app loaded from Internet从 Internet 加载应用程序时,Codeigniter REQUEST_URI 的值错误
【发布时间】:2017-08-03 13:00:39
【问题描述】:

我在 Ubuntu 17.04 服务器上安装了最新的 Apache、PHP7 和 codeigniter。我已经为它分配了一个本地 IP,比如说 10.20.30.40
这个 codeigniter 应用程序应该在本地工作,也应该在外部(互联网)工作。 还有一个中间 nginx 服务器(我无法访问)将所有内容从外部(互联网)重定向到我的服务器,所以我使用 xdebug 进行了一些测试和调试:

本地

http://10.20.30.40/ciapp/index.php/test
工作正常

$_SERVER['REQUEST_URI'] = string '/ciapp/index.php/test'

来自互联网

http://www.subdomain.company.xxx/ciapp/index.php/test
不起作用(它会抛出 codeigniter 404 页面)

$_SERVER['REQUEST_URI'] = string '//ciapp/index.php/test'

(注意前面的双斜杠)

另外,我已经测试了以下

http://10.20.30.40//ciapp/index.php/test
(注意故意使用双斜线)没用,得到 CI 的 404

我可以用 REQUEST_URI 做些什么,让它从外部工作吗?或者可能是另一个问题...?

PS:其他地方建议的配置:

$config['base_url'] = $_SERVER['HTTP_HOST'] == '10.20.30.40' ? 'http://10.20.30.40/ciapp/' : 'http://www.subdomain.company.xxx/ciapp/'; 

【问题讨论】:

  • 你的本地网络服务器是在windows下运行,而你的live服务器是在linux上运行吗?
  • @TimBrownlaw codeigniter 安装在具有静态 IP 的 Ubuntu 服务器中。本地 url 指向这个服务器,live url 也指向这个 Ubuntu 服务器,不同的是只有来自互联网的传入流量被另一个服务器(不知道操作系统)处理 nginx
  • 先检查 PHP 和 Apache 日志。

标签: php apache codeigniter nginx url-routing


【解决方案1】:

尝试基本网址

$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";

【讨论】:

  • 嗨@hrishi 在检查您的回复之前我已经成功了。无论如何都会尝试。谢谢
  • 您的解决方案有效,但只有 $config['uri_protocol'] = 'PATH_INFO';所以这似乎是解决方案,PATH_INFO
【解决方案2】:

终于通过设置uri_protocol = PATH_INFO解决了

文件: application/config/config.php

$config['base_url'] = $_SERVER['HTTP_HOST'] == '10.20.30.40' ? 'http://10.20.30.40/ci/' : 'http://www.subdomain.company.xxx/ciapp/'; 
$config['index_page'] = '';
$config['uri_protocol'] = 'PATH_INFO'; 

文件: .htaccess(从 url 中删除 index.php)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

现在它可以在本地完美运行并从 Internet 加载

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多