Skip to content

Instantly share code, notes, and snippets.

@mborodov
Forked from wnstn/router.php
Created October 4, 2018 13:48
Show Gist options
  • Save mborodov/fec94e11863b5f014a8e2a0ec48d28c9 to your computer and use it in GitHub Desktop.
Save mborodov/fec94e11863b5f014a8e2a0ec48d28c9 to your computer and use it in GitHub Desktop.

Revisions

  1. Winston Hearn created this gist Apr 16, 2013.
    18 changes: 18 additions & 0 deletions router.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <?php

    $root = $_SERVER['DOCUMENT_ROOT'];
    chdir($root);
    $path = '/'.ltrim(parse_url($_SERVER['REQUEST_URI'])['path'],'/');
    if(file_exists($root.$path))
    {
    if(is_dir($root.$path) && substr($path,strlen($path) - 1, 1) !== '/')
    {
    header('location: '.rtrim($path,'/').'/');
    exit;
    }
    if(strpos($path,'.php') === false) return false;
    else {
    chdir(dirname($root.$path));
    require_once $root.$path;
    }
    }else include_once 'index.php';