Use URL rewriting as array in .htaccess

2

Is it possible to use the .htaccess rebrand and an undefined number of parameters in the URL?

In my current .htaccess I set three types of parameters that can be passed in the URL (/ page / sub / id), however I would like to be able to pass an unlimited number of parameters and that they follow an array-like order (/ 1/2/3/4 / ...), is it possible?

My current .htaccess:

Options -Indexes
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]*)$ index.php?page=$1 [L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ index.php?page=$1&sub=$2 [L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ index.php?page=$1&sub=$2&id=$3 [L]

Doubt: Is it possible to do this without having to do some "alternative way" using explode() for example?

    
asked by anonymous 03.11.2014 / 20:39

2 answers

4

One possible output is to send all addresses to the same PHP:

Options -Indexes
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

And in this PHP, split and process the paths as needed:

<?php

   $caminho = $_SERVER["PATH_INFO"];
   $pastas = explode( '/', $caminho );

   // demonstracapo
   print_r( $pastas );

?>

If you prefer to change index.php/$1 by index.php?$1 or similar it also gives, but the path will instead of QUERY_STRING .

I particularly prefer the slash, because then the parameters of $_GET[] will function normally.

EDIT: Since OP prefers a pure solution with .htaccess , I will leave this as a reference only for anyone interested in using this path.

Note that this solution works even for those who do not know how to deal with .htaccess , simply by PHP in the path this way:

example.com/api.php/barcode/289163753
    
03.11.2014 / 21:03
0

One option you can do is set .htaccess as follows.

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1

Where will be passed to file index.php in parameter url

No index.php you recover as follows:

$url = explode('/', $_GET['url']);

And it's up to you to set the order of who's who. Ex.:

$url[0]//controller
$url[1]//action
$url[2]//parametro1
$url[3]//parametro3
    
03.11.2014 / 21:15