Get $_GET value with friendly url

Asked

Viewed 458 times

0

I have the following situation, when the URL no values after bar http://www.meusite.com/ and I type something in the search http://www.meusite.com/?s=teste includes my result page, now when the URL has values http://www.meusite.com/minha-pagina/?s=teste does not include my results page, how could I arrange this?

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
$dir = "views/arch";
if (isset($_GET['url'])) :
    $url = addslashes($_GET['url']);
    $sep = explode('/', $url);
endif;

$pg = array('contato');
if (isset($_GET['s'])) :
    require_once (''.$dir.'/search.php');
elseif (isset($sep[0]) && $sep[0] == 'filter') :
    require_once (''.$dir.'/filtro.php');
elseif (isset($sep[1])) :
    require_once (''.$dir.'/categoria.php');
elseif (isset($sep[0]) && in_array($sep[0], $pg)) :
    require_once (''.$dir.'/'.$sep[0].'.php');
else :
    require_once (''.$dir.'/home.php');
endif;

1 answer

1


tried to manipulate the directories in . htaccess? I already needed the following situation:

<IfModule mod_rewrite.c>
 RewriteEngine On

 RewriteRule ^/?([A-Za-z0-9-]+)?$ arquivo.php?busca=$1 [NC,L]

 RewriteRule ^/?([A-Za-z0-9-]+)?/$ arquivo.php?busca=$1 [NC,L]

 RewriteRule ^/?([A-Za-z0-9-]+)?/([^/.]+)/$ arquivo.php?url=$1&busca=$2[NC,L]

</IfModule> 

That way, I’ve determined exactly $_GET['search'] when it’s at the root or when it’s at another level.

Updating

When you send the variable to the root of the directory, you don’t need . htaccess to recover it.

When trying to recover by . htaccess, you use a URL parameter to recover. Example below:

.htaccess

<IfModule mod_rewrite.c> 

RewriteEngine On 

RewriteRule ^/?([A-Za-z0-9-]+)/?([A-Za-z0-9-]+)/?$ index.php?url=$1&s=$2 [NC,L] 


</IfModule>

index php.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);


echo 'Minha query de busca:'.$_GET['s'].'<br />';
echo 'Meu diretório:'.$_GET['url'].'<br />';


function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}

//QUEBRANDO E EXIBINDO OS FRAGMENTOS DA URL APÓS ?
var_dump(explode("?", curPageURL()));

?>

As you can see in the file, I also added a function if you want to, for some reason, collect the URL query in the traditional way too (not friendly as your example).

You can see the compiled result here: https://www.crm.ecowd.inf.br/teste/a/c/? search=abc

I hope it became clearer, hugs and good luck

  • In the second level, don’t you need to have Rewrite Rule with and without a bar? I added the line: <Ifmodule mod_rewrite. c> Rewriteengine On Rewritecond %{SCRIPT_FILENAME} ! -f Rewritecond %{SCRIPT_FILENAME} ! -d Rewriterule (.*)$ index.php? url=$1 Rewriterule /?([A-Za-Z0-9-]+)? $ index.php? s=$1 [NC,L] Rewriterule /?([A-Za-Z0-9-]+)? /$ index.php? s=$1 [NC,L] Rewriterule /?([A-Za-Z0-9-]+)? /([ /.]+)$ index.php? url=$1&s=$2 [NC,L] Rewriterule /?([A-Za-Z0-9-]+)? /([ /.]+)/$ index.php? url=$1&s=$2 [NC,L] </Ifmodule>

  • Won’t old <IfModule mod_rewrite.c> &#xA; RewriteEngine On &#xA; RewriteCond %{SCRIPT_FILENAME} !-f &#xA; RewriteCond %{SCRIPT_FILENAME} !-d &#xA; RewriteRule ^(.*)$ index.php?url=$1 &#xA; RewriteRule ^/?([A-Za-z0-9-]+)?$ index.php?s=$1 [NC,L] &#xA; RewriteRule ^/?([A-Za-z0-9-]+)?/$ index.php?s=$1 [NC,L] &#xA; RewriteRule ^/?([A-Za-z0-9-]+)?/([^/.]+)$ index.php?url=$1&s=$2 [NC,L] &#xA; RewriteRule ^/?([A-Za-z0-9-]+)?/([^/.]+)/$ index.php?url=$1&s=$2 [NC,L] &#xA;</IfModule>

  • 1

    @goio, I complemented the answer with an example and I think the biggest problem was the transformation of query into parameter by . htaccess; hope it helps brother, abs

Browser other questions tagged

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