Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache mod_rewrite and PHP GET Arrays

I want to have a url like http://localhost/folder1/folder2/folder3/file Then i want mod_rewrite to convert it to $_GET['d'] in the url it would look like d[]=folder1&d[]=folder2&d[]=folder3

Is this possible?

Thank You.

like image 294
mike Avatar asked Dec 30 '22 12:12

mike


1 Answers

Yes it is possible:

RewriteRule ^(([^/]+/)*)([^/]+)/([^/]+)$ /$1$4?d[]=$3 [QSA,N]
RewriteRule ^([^/]+)$ - [L]

But mod_rewrite is not really suited for that kind of work. In fact, you can quickly create an infinite loop with the N flag.

You should rather use PHP to extract the requested URL path its segments:

$path = strtok($_SERVER['REQUEST_URI'], '?');
$segments = implode('/', ltrim($path, '/'));

Then use this single rule to rewrite the requests to your file:

RewriteRule ^([^/]+/)+([^/]+)$ $2 [L]
like image 82
Gumbo Avatar answered Jan 01 '23 02:01

Gumbo