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, '/')));
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 thenREDIRECT_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.– Guilherme Nascimento
posted htaccess
– Burigo