Use URL rescrita as array in . htaccess

Asked

Viewed 391 times

2

It is possible to use rescrita with . htaccess and an undefined number of parameters in the URL?

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

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: it is possible to do this without having to do some "alternative way" using explode() for example?

  • I didn’t understand.

  • Redirect any address to a type page seletor.php?parametros and split the bars in PHP and not in htaccess.

2 answers

4


A 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, divide and process the paths as needed:

<?php

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

   // demonstracapo
   print_r( $pastas );

?>

If you’d rather trade index.php/$1 for index.php?$1 or similar gives also, but the way will come in the place of QUERY_STRING.

I particularly prefer the bar, because there the parameters of $_GET[] will work normally.

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

Noting that this solution works even for those who have no way of touching .htaccess, simply put PHP in the path this way:

example.com/api.php/barcode/289163753
  • I think this is bad, because $_GET stops working. My question is: is it possible to do this without using explode(), only with . htaccess?

  • Yes, I read it very well and I believe it works, but I would like to know if it is possible using only . htaccess which I believe is the right way to do, if not, your answer will be very useful.

  • In this case it depends on each page of the system to provide the information as necessary, but it will follow a pattern such as: /main page/sub_page/products/id/parametro1/parametro2/

  • I thought it was something like that. I can’t imagine where you would have gained so much by avoiding the explosion (the .htaccess uses regex, who tries to be much "worse" than explode in terms of algorithmic complexity), but let’s hope for more answer options (which is the cool of the site, a question can come out several legal options). I need to leave, but I think about it later (and I see the news).

0

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

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

Where it will be passed to the archive index.php in the parameter url

In the index.php you recover as follows:

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

And then it is up to you to define the order of who is who. Ex.:

$url[0]//controller
$url[1]//action
$url[2]//parametro1
$url[3]//parametro3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.