Treat request redirected by Htaccess

Asked

Viewed 226 times

3

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

I’ve 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]

Then it was easy, just take the $_GET['url'] in the index.php and treat it any way I want. But I’ve come across the following .htacesss:

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

My doubt is how to catch what was passed, since it does not play the request on $_GET['url'].

1 answer

2


One way you could do this is this, 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 suppose you accessed the following url for example:

http://localhost/seu_projeto/segmento_1/segmento_2/segmento_3

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 using the $uriParts array, you can come to handle requests made in your application.

Browser other questions tagged

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