How to make a search system with Friendly Urls

Asked

Viewed 212 times

0

Hello, I would like to do a search system with friendly Urls, I researched a lot and tried a lot of things, not yet managed.

I would like the url to be in this form site.com/home/categoria/pesquisa.

home php.

<form method="POST" class="floatLeft formSearch" action="home/go">
    <div class="formSearchDiv">
        <input type="text" name="searchInput" class="searchInput floatLeft">
        <select name="categorySearch" maxlength="255" class="categorySearch floatLeft">
                <option value="Todos" selected="selected">Todos</option>
                <option value="Serie">Serie</option>
                <option value="Filme">Filme</option>
                <option value="Anime">Anime</option>
                <option value="Filme Adulto">Filme Adulto</option>
                <option value="Desenho">Desenho</option>
                <option value="Software">Software</option>
                <option value="Jogos">Jogos</option>
        </select>
        <input type="image" name="submitSearch" src="<?=DIR_IMAGES ?>lupa.png" class="submitSearch floatLeft">
    </div>
</form>
require DIR_FUNCS.'/funcSQL.php';
$sql = new SQL();

if(isset($_POST['submitSearch']))
{   
    if(isset($_GET['go']))
    {
        if(preg_match("^/[A-Za-z]+/", $_POST['searchInput']))
        {
            $search = $_POST['searchInput']; 
            echo $search;
            $sql->listTorrentSearch($search, "Todos");
        }
    }
}

Funcsql.php

public function listTorrentSearch($search, $category) 
{
    $conn = $this->openSQL();
    $cont = 0;

    if($category == "Todos")
    {
        $result = mysqli_query($conn, "SELECT * FROM torrents WHERE Nome LIKE '".$search."%'");
    }
    else
    {
        $result = mysqli_query($conn, "SELECT * FROM torrents WHERE Nome LIKE '".$search."%' AND Categoria='".$category."'");
    }

    while($row = mysqli_fetch_array($result))
    {

        if($cont < 25)
        {
        $name = $row['Nome'];
        $category = $row['Categoria'];
        $size = $row['Size'];
        $magnet = $row['Magnet'];

        $createTorrentClass = new CreateTorrent();

        $createTorrentClass->createTorrentDiv($name, $category, $size, $magnet);

        $cont++;
        }
    }

    mysqli_close($conn);
}

.htacess

RewriteEngine On

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

RewriteRule ^(.*)$ index.php?url=$1

treatUrl.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';
}

1 answer

0


Lucas,

In that blog has a very complete explanation, but in short you should do the following:

You need to mark the current addresses of your site and how you want them to look. Follow a pattern to keep the same information organized. Examples:

Let’s start with the example of the contact page you currently access by the address http://www.meusite.com.br/contato.php.

Go to the root of your site and edit or create a file called .htaccess. Place the following lines:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^contato/?$ /contato.php [NC,L]
</IfModule>

Ifmodule is like a condition block that checks if a certain module exists and is enabled.

The rule is written by Rewriterule:

RewriteRule ^contato/?$/contato.php [NC,L]

The validation of the URL is done via regular expression, the circumflex means "start", the dollar sign means "end" and the part /? means an optional bar, that is: The rewrite will happen for the Urls meusite.com.br/contact and meusite.com.br/contact/.

In the /contact.php part we inform the server which file will meet the request. Note that this is the part where we tell the server what the format of our old URL is.

In part [NC,L] there are flags, the NC of "in the case" (ignores the difference between upper and lower case) and L of "last" means that if this rule is used, no other rule is used.

If you want to learn more about flags, I recommend reading the Apache documentation.

If you want to work with more complex Urls, which contain variable values like meusite.com/products/tennis/ check here:

  • Hello, I took a look at the tutorial when I use the rule RewriteRule ^(.*)$ index.php?s=3&c=5 and access site.com/?s=3&c=2 he returns 3 e 5, but when I use the rule RewriteRule ^(.*)$ index.php?s=$1&c=$2 and access the same site, it returns index.php e vazio.

  • @Lucascarezia you want to redirect site.com/? s=3&c=2 to what? To site.com/index/ or site.com/3/2 or other destination?

  • Rewriterule index/([0-9-]+)/([0-9]+)/? $ /index.php? s=$1&c=$2 [NC]

  • The above example should turn your URL into index/1/2/

Browser other questions tagged

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