Skip to content

Instantly share code, notes, and snippets.

@Stilg4r
Created August 26, 2021 11:53
Show Gist options
  • Save Stilg4r/aaa96156660e890f9e62587b01a5c651 to your computer and use it in GitHub Desktop.
Save Stilg4r/aaa96156660e890f9e62587b01a5c651 to your computer and use it in GitHub Desktop.
Ejemplo básico de router
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(fonts|images|img|css|js|robots\.txt)
RewriteRule ^(.*)$ /router.php?url=$1 [NC,L]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1>Hello, world!</h1>
<a href="./url_amigable">url_amigable</a>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-U1DAWAznBHeqEIlVSCgzq+c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj" crossorigin="anonymous"></script>
</body>
</html>
<?php
$routes=[
// la ruta esta definida por una exprecion regular como esta
'/^GET\/$/i'=>['script' => './index.php'],
'/^GET\/url_amigable$/i'=>['script' => './otro_script.php'],
];
$url = "{$_SERVER['REQUEST_METHOD']}/{$_GET['url']}";
foreach ($routes as $route => $destination) {
if (preg_match($route, $url)) {
require_once($destination['script']);
exit();
}
}
require_once('./404.php');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment