I have a website that use this style : /index.php?page=45&info=whatever&anotherparam=2

I plan to have pretty url to transform the previous url to : /profile/whatever/2

I know I have to use .htAccess and to redirect everything to index.php. That's fine.

My problem is more in the index.php (Front Controller). How can I build back the $_GET["info"] and $_GET["anotherparam"] to be able to continue to use all the existing code that use $_GET[...] in their page?

Do I have to build back the GET in the header with some code or do I have to get rid of all $_GET[...] on every pages by creating my own array that will parse ever / to and assign something like : $myParam["info"] = "whatever" and than in the page use $myParam[] instead of $_GET[] ?

I would like not to modify all those pages that use $_GET[]


My .htAccess look like:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ index.php [NC,L]

Everything that doesn't exist go to index.php. Since I already use this structure : /index.php?page=45&info=whatever&anotherparam=2 nothing is broken. But now I will use /profile/whatever/2 and in the switch case I can determine what page to include(..) but the problem is with all GET parameters. How do I build them to have access from all page with $_GET[]?

Accepted Answer

$path = ... // wherever you get the path $_SERVER[...], etc.
            // eg: /profile/wathever

$segments = split ($path);

$segments_name = Array ('page', 'info', 'anotherparam');
for($i=0;$i  < count ($segments); $i++) {
  $_GET[$segments_name[$i]] = $segments[$i];

with this solution you have to always use the same arguments at the same position

if you don't want that you have two solution: - use path like /page/profile/info/wathever - use router system (for that I recommend you to use a framework instead of doing it all manually)

edit: second solution

$path = ... // wherever you get the path $_SERVER[...], etc.
            // eg: /page/profile/info/wathever
$segments = split ($path);

for($i=0;$i  < count ($segments); $i+=2) {
  $_GET[$segments[$i]] = $segments[$i+1];
Written by mathroc
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki