Pretty url for string parameters

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

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

I know that I need to use .htAccess and redirect everything to index.php. It's great.

My problem is more with index.php (Front Controller). How can I recover $_GET["info"]and $_GET["anotherparam"]continue to use all existing code that I use $_GET[...]on my page?

Do I need to create a GET in the header with some code, or do I need to get rid of everything $_GET[...]on all pages by creating my own array that will parse ever /and assign something like: instead of $myParam["info"] = "whatever"using the page $myParam[]instead $_GET[]?

I would not want to change all pages using $_GET[]

Edit:

My .htAccess looks like this:

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

Everything that does not exist goes to index.php. Since I already use this structure: /index.php?page=45&info=whatever&anotherparam=2nothing is broken. But now I will use it /profile/whatever/2, and in the case of the switch, I can determine which page include(..), but the problem is with all the GET parameters. How to create them for access from the whole page using $ _GET []?

+3
source share
2 answers
$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 should always use the same arguments in the same position

, :  - , /page/profile/info/wathever  - ( , )

edit:

$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];
}
+3

switch. .htaccess

<?php
switch ($_GET) {
    case "home":
     header('Location: /home/');
        break;
    case "customer":
        header('Location: /customer/');
        break;
    case "profile":
        header('Location: /profile/');
        break;
}
?>
0

Source: https://habr.com/ru/post/1735119/


All Articles