> ## Documentation Index
> Fetch the complete documentation index at: https://reduardo.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Rutas Amigables

> URLs limpias con .htaccess y router.php

# Rutas Amigables

El sistema implementa URLs limpias (sin `index.php?route=`) usando `.htaccess` para Apache y `router.php` para el servidor PHP built-in.

## Antes vs Después

| Antes | Después |
| - | - |
| `index.php?route=catalogo` | `/catalogo` |
| `index.php?route=productos` | `/productos` |
| `index.php?route=productos/edit&id=5` | `/productos/edit?id=5` |
| `index.php?route=login` | `/login` |

## Implementación

### `.htaccess` (Apache / XAMPP)

```apache theme={null}
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
```

Redirige todas las solicitudes a `index.php`, excepto archivos y directorios existentes (como imágenes en `uploads/`).

### `router.php` (PHP Built-in Server)

```php theme={null}
<?php
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$file = __DIR__ . $path;
if (is_file($file)) {
    return false;
}
require_once __DIR__ . '/index.php';
```

Se usa con: `php -S localhost:8000 router.php`

### Front Controller (`index.php`)

Parsea la URL del `REQUEST_URI` y determina la ruta:

```php theme={null}
$basePath = dirname($_SERVER['SCRIPT_NAME']);
$requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$path = substr($requestUri, strlen($basePath));
$route = trim($path, '/');
```

## Constante `BASE_URL`

Se define automáticamente para generar URLs correctas desde cualquier subdirectorio:

```php theme={null}
define('BASE_URL', rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\'));
```

Uso en las vistas:

```php theme={null}
<a href="<?= BASE_URL ?>/productos">Productos</a>
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.