4

I run two Apache 2 servers. One has PHP5.2 and the other has PHP5.3. Is there a reason why on the 5.3 machine has $_SERVER['SCRIPT_URI']?

Where does this variable come from? It is clearly something that is coming through from the Apache environment and it is not documented in the PHP manual. It is however a handy shortcut over a combination of ['HTTPS'], ['SERVER_NAME'] and ['REQUEST_URI'].

I have tried looking through configuration files, searching SO and the web.

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • I assume you mean "and the other has 5.3"? as for your question, sorry but I'm not sure. longshot but have you had a look round the apache2 docs? AFAIK alot of what is available in the $_SERVER[] global is determined by the server itself. odd that the manual doesn't document it... 5.3 has been out for a while now hasn't it? – totallyNotLizards Dec 06 '11 at 10:36
  • Thanks I have fixed that version number! – Treffynnon Dec 06 '11 at 10:38

3 Answers3

9

According to a post on WebHostingTalk it comes from mod_rewrite:

Add

RewriteEngine On

To the virtual host in your httpd.conf file that you want to turn this on for and then restart apache.

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
Paté
  • 1,914
  • 2
  • 22
  • 33
  • No that didn't do the trick. The rewrite engine is on on my system and my Webpage is inside a virtual host. But nonetheless it didn't work. – flu Sep 03 '12 at 08:04
  • This worked for me. I had to set it in the site's http config file. – dmgig Oct 17 '16 at 19:18
4

I moved to CentOS 7 with Plesk12, PHP 5.6.6 and rewrite on etc. SCRIPT_URI has not been there. And because it is so nice to use in some situations I wrote this workaround:

if(!isset($_SERVER['SCRIPT_URI'])){
    $_SERVER['SCRIPT_URI'] = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

    $pos = strrpos($_SERVER['SCRIPT_URI'],'/');
    if($pos!==false) {
        $_SERVER['SCRIPT_URI'] = substr($_SERVER['SCRIPT_URI'], 0, $pos+1);
    }
}

As I am not a total expert, please review this code to your specific application before plugging it in. On my system it seems to work perfectly. I just put it in the Head-Area of my index.php and others.

Nibbels
  • 41
  • 1
0

As far as I know $_SERVER['SCRIPT_URI'] is only available if you're running PHP as a CGI. I suppose that must be the difference in your two PHP installations.

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
Michi
  • 2,480
  • 3
  • 28
  • 38