How to pick variable with $_GET

Asked

Viewed 85 times

1

Hello, in my htaccess is like this:

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d

RewriteRule ^(.*)$ index.php?url=$1
RewriteRule ^download/([A-Za-z0-9]+)$ index.php?a=download&k=$1 [QSA,L]

And no download.php

<?php
$k = $_GET['k'];
echo $k;

But it returns this error when I access download/?k=aaaa or download/?aaaa

Notice: Undefined index: k

index php.

<body>
    <?php
        require 'config/tratarUrl.php';
        include $pag;
    ?>
</body>

treatUrl.php

<?php
$pUrl = strip_tags(trim(filter_input(INPUT_GET, 'url', FILTER_DEFAULT)));
$sUrl = (empty($pUrl) ? "index" : $pUrl);
$url = array_filter(explode('/', $sUrl));

if (count($url) > 1) 
{
$cont = 1;
foreach ($url as $arg) 
{
    define("PARAM" . $cont, $arg);
    $cont++;
}
} 
else if (count($url) == 1) 
{
if (file_exists(DIR_PAGES . $url[0] . '.php')) 
{
    $pag = DIR_PAGES . $url[0] . '.php';
} 
else 
{
    if($url[0] != 'index')
    {
        $pag = DIR_PAGES . '404.php';
    }
    else
    {
        $pag = DIR_PAGES . 'home.php';
    }
}
} else {
$pag = DIR_PAGES . '404.php';
}
  • What returns with var_dump($_SERVER["QUERY_STRING"]);?

  • Returns string(13) "url=download/, Strange he’s not picking up the aaaa in the end.

  • I believe your rule should be RewriteRule ^download/([A-Za-z0-9]+)$ index.php?a=download&k=$1 [QSA,L]

  • Keeps returning the same error Undefined index: k and same message string(13) "url=download/"

  • I already changed the question.

  • Change that line # RewriteRule ^(.*)$ index.php?url=$1 with the # at the beginning; this rule is taking everything (.*)

Show 1 more comment

1 answer

1


Your rule must contain [QSA,L].

The flag QSA means you will accept a sequence for consultation.

The flag L means that if the rule matches, it will not process the next string


Change the order of your rule:

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d

RewriteRule ^download/([A-Za-z0-9]+)$ index.php?a=download&k=$1 [QSA,L]
RewriteRule ^(.*)$ index.php?url=$1

  • Now when I type site.com/download/?aaaa returns Objeto não encontrado!

  • I edited to ask you to understand better.

  • But your rule for download is site.com/download/arquivo, and not site.com/download/?aaaa. Following the rule logic, the parameter a is download, and the parameter k corresponds to the file.

  • When I walk in site.com/download/aaaa it literally accesses the file /aaaa, but I would like when to access site.com/download/aaaa i just took the "aaaa" information but kept going into the file download.

  • Apparently because of your loop. See if this reply, or Daniel’s help in your code.

  • 1

    I managed to solve my problem, however another doubt arose, so that this topic is not overloaded I will create a new.

Show 1 more comment

Browser other questions tagged

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