The PHP global variable $_SERVER['PATH_INFO'] contains the path suffixed to a PHP script, if I would call the URL:


Then $_SERVER['PATH_INFO'] would contain:


Traditionaly the $_GET variables are used for certain parameters like a page to display:


This method is easy to program, but not only looks strange, but also is very search engine unfriendly. Most searchengines ignore the QueryString (the part of the URL after the ?). And therefor would index the first page.php?page=x they would find and ignore the rest.
Some searchengines like Google do not ignore the query string, but would give a page without using a querystring for different content a way higher ranking.

Parsing the $_SERVER['PATH_INFO'] is relatively easy, this code would do most of the stuff just fine:

if (!isset($_SERVER['PATH_INFO'])){
	$pathbits= array('');
	$pathbits = explode("/",  $_SERVER['PATH_INFO']);

The $pathbits array would always contain / as first element if a path info was provided, otherwise it will be an empty array.

Here is a quite simple example which parses the path info to decide which file to include:

if (!isset($_SERVER['PATH_INFO'])){
	$pathbits= array('');
	$pathbits = explode("/",  $_SERVER['PATH_INFO']);
if (!isset($pathbits[1]) || $pathbits[1] == ""){
	$page = "default"
	$page = basename($pathbits[1]);
$file = "./pages/{$page}.php";
if (!is_file($file)){
	echo "File not found";
	require $file;

4 thoughts on “Parsing $_SERVER[‘PATH_INFO’]”

  1. You must provide $default with an array containing the values you want to extract as key and the default value for each.

    By example, to extract the page topic, book and page and the number with this path: ‘/support/bash/8’ you must pass $default with
    array(‘topic’ => ‘defaulttopic’,’book’=>’defaultbook’,’page’=>1)

    The following elementh in the path will be returner in a name = value pairs as follows: (/support/bash/8/link/title1)

    ‘topic’ => ‘support’,
    ‘book’ => ‘bash’,
    ‘page’ => 8,
    ‘link’ => ‘title1’,


    function router($default)
    $keys = array_keys($default);
    $pos = 0;
    $res = array();
    $name = isset($keys[$pos])?$keys[$pos]:”;
    foreach(explode(‘/’,isset($_SERVER[‘PATH_INFO’])?$_SERVER[‘PATH_INFO’]:”) as $value){
    if($value != ”){
    if($name == ”){
    $name = $value;
    $res[$name] = $value;
    $name = isset($keys[$pos])?$keys[$pos]:”;
    if($name != ”){ // Si se pasó un parámetro pero no su valor
    $res[$name] = ”;
    foreach($default as $key => $value){ // Valores default, por si no existen
    if(!isset($res[$key]) || ($res[$key] == ”)){
    $res[$key] = $value;
    return $res;

  2. correction..
    if (!isset($pathbits[1]) || $pathbits[1] == “”){
    $page = “default”

    missing semicolon – should read
    $page = “default”;

  3. I was using Ruby but I was glad to find your article. It really shouldn’t be that hard to find what path_info means on Google. *sigh*

    Thank you for your concise article.

Leave a Reply

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