Skip to content

Instantly share code, notes, and snippets.

@tobiasroeder
Last active January 15, 2023 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobiasroeder/aded6501ae404596d4843a496b5214fa to your computer and use it in GitHub Desktop.
Save tobiasroeder/aded6501ae404596d4843a496b5214fa to your computer and use it in GitHub Desktop.
Very simple PHP Router
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<?php
define('VIEW_DIR', __DIR__ . '/views');
require_once __DIR__ . '/router.php';
<?php
// inspired by: https://www.taniarascia.com/the-simplest-php-router/
/**
* PHP Router
*/
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = rtrim($uri, '/');
switch ($uri) {
case '':
case '/':
require VIEW_DIR . '/home.php';
break;
case '/about':
require VIEW_DIR . '/about.php';
break;
case '/projects':
require VIEW_DIR . '/projects.php';
break;
default:
http_response_code(404);
require VIEW_DIR . '/404.php';
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment