How to make a search system with Friendly Urls

0

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

I would like the url to be of 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';
}
    
asked by anonymous 22.02.2017 / 14:40

1 answer

0

Lucas,

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

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

Let's start with the example of the contact page that you currently visit at link .

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

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

IfModule is like a condition block that checks to see 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 caret ^ means "start", the dollar sign means "end" and the part /? means an optional bar, ie: The rewriting will happen for the URLs mysite.com / contact and mysite.com / contact /.

In the /contato.php part we inform the server which file will respond to the request. Note that it is in this part that we tell the server what the format of our old URL is.

In the [NC,L] part there are flags, NC in case " (ignores case sensitivity) and L

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 the example myite.com/products/tenis/ check here:

22.02.2017 / 15:38