The Simplest PHP Router - Tania Rascia
The Simplest PHP Router - Tania Rascia
.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]
index.php
<?php
$request = $_SERVER['REQUEST_URI'];
switch ($request) {
case '/' :
require __DIR__ . '/views/index.php';
break;
case '' :
require __DIR__ . '/views/index.php';
break;
case '/about' :
require __DIR__ . '/views/about.php';
break;
default:
http_response_code(404);
require __DIR__ . '/views/404.php';
break;
}
/views/index.php
<h1>Main</h1>
/views/about.php
<h1>About</h1>
/views/404.php
<h1>404</h1>
That's it!
Tags
I'm a software developer who makes open-
source projects and writes about life, code, php snippets router
design, and more. This site is and has
always been free of ads, trackers, social
Subscribe to the Newsletter
media, affiliates, and sponsored posts.
Comments