0

I have urls that look like this ($_GET):

http://localhost/index.php?search=param&other1=param1&other2=param2&other3=param3&other4=param4

Based on SPA Structure:

where search: is a controller for searches; which can receive additional parameters.

where Other: 1,2,3,4,5 are additional parameters, there is not a defined number of parameters, they can be less or more.

I was thinking of creating a friendly url something like:

http://localhost/search=param/other1=param1/other2=param2/other3=param3/other4=param4

The htaccess i started testing looks like this:

php_value display_errors On
php_value mbstring.http_input auto

<IfModule mod_rewrite>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

and I have 2 questions:

  1. Is it correct, maybe, or can it be improved even more?
  2. How do I achieve it?

I have seen previous answers suggested by the site's search engine but they are far from what I hope to do:

https://stackoverflow.com/a/45075219/20284348
Problems configure .htaccess for friendly url
Taxonomy url change to friendly url
https://stackoverflow.com/a/55696789/20284348

They do not seem valid to me or may even be obsolete.

1 Answers1

1

If you don’t know which parameters will be provided, you should provide the full string in a single GET parameter and parse it in your PHP code.

RewriteRule ^(.*)$ index.php?params=$1 [L]

Index.php:

<?php
if ( isset( $_GET['params'] ) ) {
    // splits the slash-separated params string into an array
    $strParams = explode('/', $_GET['params']);

    foreach ( $strParams as $strParam) {
        $matches = [];
        // look for `=` char to separate parameter name and value
        preg_match('/([^=]*)=(.*)/', $strParam, $matches);

        // Populate $_GET using parameter name as key
        $_GET[$matches[1]] = $matches[2];
    }
}

I directly populated the superglobal $_GET, but a properer way would be to set other variables which would be used instead of $_GET.

Pols12
  • 102
  • 2