REQUEST_URI or REDIRECT_QUERY_STRING?

0

Well, I'm doing a friendly url class and in the file CONFIG.PHP I gave a define in the following

define('UA',explode('/',$_SERVER['REDIRECT_QUERY_STRING']??null));

But when I type the existing file, for example% w / the product file exists . When I give var_dump it returns an array with an empty string, but if I use this method:

define('UA',explode('/',$_SERVER['REQUEST_URI']??null));

It returns me several arrays with the right placements, but at index ('teste.com/produto/iphone-7-64gb/35') it returns me an empty string. I wanted a solution because in the video lesson I'm watching the method 'REDIRECT_QUERY_STRING worked perfectly.

.Htaccess

  RewriteEngine On
  RewriteCond %{SCRIPT_FILENAME} !-f
  RewriteCond %{SCRIPT_FILENAME} !-d
  RewriteRule ^(.*)$ ?$1
    
asked by anonymous 30.01.2018 / 18:25

1 answer

1

The REDIRECT_QUERY_STRING is generated exclusively by .htaccess and will only get querystring, REQUEST_URI will get the URL + querystring.

Note that REDIRECT_QUERY_STRING is something that is being generated at each internal redirect (rewriting), so it can generate something like REDIRECT_REDIRECT_QUERY_STRING and then REDIRECT_REDIRECT_REDIRECT_QUERY_STRING , depending on how many redirections it takes.

What you can do to ease is simply to pass as a GET parameter, like this:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ ?PATH=$1 [L]

And in PHP I'd get it like this (php7):

define('UA', explode('/', $_GET['PATH'] ?? ''));

In php5:

define('UA', explode('/', empty($_GET['PATH']) ? '' : $_GET['PATH']));

PHP_SELF

Another way to avoid controlling the PATH parameter would be to use the PHP_SELF combined with parse_url , first the HTACCESS should look like this:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^ index.php [L]

So PHP should look like this:

$path = parse_url($_SERVER['PHP_SELF'], PHP_URL_PATH); //isto irá remover a querystring do sufixo

define('UA', explode('/', $path));

Of course, $_SERVER['PATH_INFO'] , but I believe that PATH_INFO is not standard and may vary (depending on the version of Apache, in this case I recommend that you use even PHP_SELF combined with parse_url .

In case the result of UA to teste.com/produto/iphone-7-64gb/35 will be something like:

Array
(
    [0] => 
    [1] => produto
    [2] => iphone-7-64gb
    [3] => 35
)

Note that [0] is empty, this is because the PATH of a URL always starts with / , so to avoid it you can simply use ltrim or substr , for example:

$path = parse_url($_SERVER['PHP_SELF'], PHP_URL_PATH); //isto irá remover a querystring do sufixo

define('UA', explode('/', ltrim($path, '/')));
    
30.01.2018 / 19:05