REQUEST_URI or REDIRECT_QUERY_STRING?

Asked

Viewed 346 times

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 ('teste.com/produto/iphone-7-64gb/35') the product file exists. When I give the var_dump it returns an array with an empty string, however if I use this method:

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

It returns me several arrays with the right positions, but in the index UA[0] it returns me an empty string. I wanted a solution, because in the video class I am watching the method `REDIRECT_QUERY_STRING worked perfectly.

.Htaccess

  RewriteEngine On
  RewriteCond %{SCRIPT_FILENAME} !-f
  RewriteCond %{SCRIPT_FILENAME} !-d
  RewriteRule ^(.*)$ ?$1
  • REDIRECT_QUERY_STRING is generated exclusively by . htaccess and will only get querystring, REQUEST_URI will get the URL+querystring. It is worth noting that REDIRECT_QUERY_STRING is something that is generated at each internal (rewrite) redirect, so it can generate something like REDIRECT_REDIRECT_QUERY_STRING and then REDIRECT_REDIRECT_REDIRECT_QUERY_STRING, depending on how many redirects occur. Ideally, you can post your HTACCESS to understand what you have done and what you really need to be able to even offer a more organized solution.

  • posted htaccess

1 answer

1


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

It is worth noting that the REDIRECT_QUERY_STRING is something that is being generated at each internal (rewrite) redirect, so it can generate something like REDIRECT_REDIRECT_QUERY_STRING and then REDIRECT_REDIRECT_REDIRECT_QUERY_STRING, depending on how many redirects occur.

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

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

And in PHP it would look like this (php7):

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

In php5:

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

PHP_SELF

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

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

Then 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 (come not to work) depending on the version of Apache, in this case I recommend that you use even PHP_SELF combined with parse_url.

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

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

Note that the [0] is empty, this is because the PATH of a URL always starts with /, then 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, '/')));
  • 1

    Ahhh thanks friend!! It became much clearer now!! note 100000

Browser other questions tagged

You are not signed in. Login or sign up in order to post.