How to use “path info” with PATH_INFO / ORIG_PATH_INFO in Apache Php Setup

By | May 15, 2023

The PATH-INFO is the extra path data that follows the name of a script (or servlet) in the URL. Its different from query string. Here is a classic example:

https://www.examples.com/index.php/some/path?a=b

Now, over here we are actually invoking the script index.php only. However also providing some path-info data "/some/path", which is then followed by the query string.

In modern mvc based php frameworks like codeignitor, symfony, laravel, path-info data is commonly used to select the class/controller and method to invoke on the server side. The use of path-info has a lot of benefits.

AcceptPathInfo Directive

The AcceptPathInfo directive is a useful feature of apache that 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.

Inside PHP

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

<?php

print_r($_SERVER);

Could show something like this:

[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

Note the variable [PATH_INFO] => /class_name/method. This particular variable information can be accessed inside php script and used to invoke the correct class-method code.

$path_info = $_SERVER['PATH_INFO'];

// Do something with the path info

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

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

Leave a Reply

Your email address will not be published. Required fields are marked *