PATH_INFO, ORIG_PATH_INFO, APACHE and PHP

AcceptPathInfo Directive

The AcceptPathInfo directive is a useful feature of apache. It detects any path information in a url following the actual script name and passes it to php as an environment variable in $_SERVER superglobal.

Php mvc based frameworks often use such urls that have additional path information after a script name. This is necessary so that the application can have a single script entry point and rest of the code can be structured inside classes in a modular fashion. If you have worked with php frameworks like codeigniter then you should be knowing it.

Lets take a few examples of how it works. Say you have a url like this in your php application

www.site.com/index.php/class_name/method?id=15

In the above url the index.php is the real script that will be called upon opening the url in the browser. After the script name there are 2 components. One is the GET parameters that follows after the question mark. The other is the 'path information' that appears between the script name and the get parameters. Its 'class_name/method' in the above url.

It is this part of the url that the apache pathinfo directive makes available to php in the $_SERVER super global. This additional path information or URI is used to specify the resource to load. In most cases its the php class to instantiate and method to call. The method then generates the rest of the html page. The php class being instantiated is called the controller, the C of MVC.

On Localhost

index.php
print_r($_SERVER)

shows :

     [SERVER_SIGNATURE] => <address>Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.7 with Suhosin-Patch Server at localhost Port 80</address> 
     [SERVER_SOFTWARE] => Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.7 with Suhosin-Patch
    [SERVER_NAME] => localhost
    [SERVER_ADDR] => 127.0.0.1
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => 127.0.0.1
    [DOCUMENT_ROOT] => /var/www/
    [SCRIPT_FILENAME] => /var/www/project/index.php
    [REMOTE_PORT] => 39733
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => id=15
    [REQUEST_URI] => /project/index.php/class_name/method?id=15
    [SCRIPT_NAME] => /project/index.php
    [PATH_INFO] => /class_name/method
    [PATH_TRANSLATED] => /var/www/class_name/method
    [PHP_SELF] => /project/index.php/class_name/method

Next using .htaccess to redirect localhost/project/class_name/method to localhost/project/index.php/class_name/method and again viewing the $_SERVER variable with url : localhost/project/class_name/method






shows :

     [SERVER_SIGNATURE] => <address>Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.7 with Suhosin-Patch Server at localhost Port 80</address> 
     [SERVER_SOFTWARE] => Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.7 with Suhosin-Patch 
    [SERVER_NAME] => localhost
    [SERVER_ADDR] => 127.0.0.1
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => 127.0.0.1
    [DOCUMENT_ROOT] => /var/www/
    [SCRIPT_FILENAME] => /var/www/project/index.php
    [REMOTE_PORT] => 39734
    [REDIRECT_QUERY_STRING] => id=15
    [REDIRECT_URL] => /project/class_name/method
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => id=15
    [REQUEST_URI] => /project/class_name/method?id=15
    [SCRIPT_NAME] => /project/index.php
    [PATH_INFO] => /class_name/method
    [PATH_TRANSLATED] => /var/www/class_name/method
    [PHP_SELF] => /project/index.php/class_name/method

In both the above PATH_INFO is identical results , REQUEST_URI is different.

On Webserver
Now when the same code was put up on the webserver ::::

without .htaccess redirect , url like domain.com/project/index.php/class_name/method?id=15

shows :

    [SERVER_SOFTWARE] => Apache/2.2.13 (Unix) mod_ssl/2.2.13 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
    [PATH_INFO] => /class_name/method
    [PATH_TRANSLATED] => /home/celcscom/public_html/project/index.php
    [QUERY_STRING] => id=15
    [REDIRECT_STATUS] => 200
    [REMOTE_ADDR] => 59.93.245.243
    [REMOTE_PORT] => 60157
    [REQUEST_METHOD] => GET
    [REQUEST_URI] => /project/index.php/class_name/method?id=15
    [SCRIPT_FILENAME] => /home/celcscom/public_html/project/index.php
    [SCRIPT_NAME] => /project/index.php
    [SERVER_ADDR] => 216.67.245.98
    [SERVER_PORT] => 80
    [PHP_SELF] => /project/index.php/class_name/method

PATH_INFO is same as localhost.

The difference came when using .htaccess redirect , the output

shows :

    [SERVER_SOFTWARE] => Apache/2.2.13 (Unix) mod_ssl/2.2.13 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
    [QUERY_STRING] => id=15
    [REDIRECT_QUERY_STRING] => id=15
    [REDIRECT_STATUS] => 200
    [REDIRECT_UNIQUE_ID] => SsI359hD9WIAAHMWpgEAAAAD
    [REDIRECT_URL] => /project/class_name/method
    [REQUEST_METHOD] => GET
    [REQUEST_URI] => /project/class_name/method?id=15
    [SCRIPT_FILENAME] => /home/celcscom/public_html/project/index.php
    [SCRIPT_NAME] => /project/index.php
    [SERVER_ADDR] => 216.67.245.98
    [ORIG_PATH_INFO] => /class_name/method
    [ORIG_PATH_TRANSLATED] => /home/celcscom/public_html/project/index.php
    [PHP_SELF] => /project/index.php

PATH_INFO is missing but ORIG_PATH_INFO is there with the information PATH_INFO was supposed to have.

So the php code had to be changed to check for $_SERVER['ORIG_PATH_INFO'] when $_SERVER['PATH_INFO'] is absent

PATH_INFO relies on the Apache AcceptPathInfo Directive

Last Updated On : 2nd June 2013

Subscribe to get updates delivered to your inbox

About Silver Moon

Php developer, blogger and Linux enthusiast. He can be reached at [email protected]. Or find him on

Leave a comment