Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
*/
$request = $_SERVER['PATH_INFO'] ?? $_SERVER['REQUEST_URI'];
$request = rtrim($request, '/');
switch ($request) {
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