Treat request redirected by Htaccess

3

I'm studying routing, trying to (re) create a routing solution of mine.

I have always used the following .htaccess to redirect my requests:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [L]

That was easy, just get the $_GET['url'] no index.php and treat as I'd like. But I've come across the following .htacesss :

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

My question is how to take what was passed, since it does not throw the request in $_GET['url'] .

    
asked by anonymous 20.11.2015 / 17:25

1 answer

2

One way you could do this is as follows, where .htaccess would look like this:

RewriteEngine on

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

And the php script would look like this:

<?php

$uri = $_SERVER['REQUEST_URI'];
$uriParts = explode('/', $uri);

var_dump($uriParts);

So, supposing that you access the following url, for example:

link

It would result in the following output:

array (size=5)
  0 => string '' (length=0)
  1 => string 'seu_projeto' (length=11)
  2 => string 'segmento_1' (length=10)
  3 => string 'segmento_2' (length=10)
  4 => string 'segmento_3' (length=10)

So in this way using the $ uriParts array, you can process the requests made in your application.

    
20.11.2015 / 18:43