Hello, it happens that I am creating a REST API in PHP and I do not want to use a framework but I have the following problem
I want to create a request by the GET method in which I send the ID of a product to search for it in the database.
Namely:
http://localhost/ws_php/productos/1
the problem is that I implement a .htaccess so that all the requests reach the index.php with the following code that I found on the web
RewriteEngine On
#RewriteRule ^([^/]*)/([^/]*)$ index.php?url=$1&username=$2 [L]
RewriteRule ^([^/]+)/? index.php?url=$1 [L,QSA]
when I print the variable $_GET['url'];
to search for the ID that I sent, it only appears up to "products" without the ID
php code
<?php
require_once('db.php');
if ($_SERVER['REQUEST_METHOD']=="GET"){
$var = $_GET['url'];
print_r($var);
}else if($_SERVER['REQUEST_METHOD']=="POST"){
echo" POST METHOD ";
}else{
http_response_code(405);
}
?>
All requests go to the index except those pointing to the images folder
localhost/ws_php/img/miimagen.jpg
The regular expression matches
^([^/]+)/?
only the first few characters that are not a/
.If we want all the routes that do not exist to go to the index, even if they are multiple folders:
If still, all the routes, even the existing ones (images for example) have to go to the index, we create two rules: one for when the index is accessed, it does not process any more rules; another so that all the rest go to the index.