How to use Friendly Urls in GET method search

Asked

Viewed 477 times

0

I’d like to make that link:

meusite.com/?searchInput=Eu+fui+viajar&categorySearch=Filme&submitSearch.x=19&submitSearch.y=17

in

meusite.com/category=Filme&search=Eu+fui+viajar

.htacess:

RewriteEngine On


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

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

php_value post_max_size 2000M
php_value upload_max_filesize 2500M
php_value max_execution_time 6000000
php_value max_input_time 6000000
php_value memory_limit 2500M

I don’t know much about Friendly Urls, so I have no idea how to do it, I took a look on the internet, but I didn’t find anything like what I want.

Form:

   <form method="GET" class="floatLeft formSearch">
    <div class="formSearchDiv">
        <input type="search" 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>

Get:

if(isset($_GET['searchInput']))
{   
    $pesquisa = $_GET['searchInput'];
    $categorySearch = $_GET['categorySearch'];
    //aqui chamo o metodo SQL para separar pela pesquisa e categoria
}
  • Lucas, good morning. <br> Please show the rest of the form code.

  • Lucas, good morning. Please show the rest of the form code.

  • @Thiagosantos I already asked the question

  • Wouldn’t a url with vertical bars be better? Type meusite.com/Category/filme/search/eu-fui-viajar

  • @Maxrogério But how to do it automatically ??

  • @Lucascarezia The model MVC fits right with your project.

  • @Lucascarezia So you use a mvc framework in php I know Cakephp, Zend and Laravel

Show 2 more comments

3 answers

1

I have a little project that I am developing for knowledge purposes in which the user url method is as follows:

.htaccess

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1

Taking the values from the url

php file.

<?php
   if(isset($_GET['url']) && !empty($_GET['url'])){
     $url = array_filter(explode("/", $_GET['url']));
     print($url);
   }
 ?>

Ready! Now you have one array with the values that were passed via url. In your case, as you have meusite.com/category=Filme&search=Eu+fui+viajar

Instead of passing your url like this...

You would pass the url as follows: meusite.com/category/Filme/search/Eu+fui+viajar

and on exit (print_r($url)) of the variable would appear:

Array ( [0] => category [1] => filmes [2] => search [3] => Eu fui viajar )

Just work with the values you got in the array

  • Hello, I didn’t quite understand how to apply this code snippet, I tried to put it just below where I pick up the search(if(isset($_GET['searchInput']))), but it did not result in anything.

  • As I said, the variable $url would flip an array, then just take the values inside the array, ex: echo $pesquisa = $url[3];, will print on screen Eu fui viajar, quoting again Basta agora trabalhar com os valores que você obteve no array

0

Lucas, try it this way:

form:

<form method="GET" class="floatLeft formSearch">
<div class="formSearchDiv">
    <input type="search" name="search" class="searchInput floatLeft">
    <select name="category" 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>
    <img src="<?php=DIR_IMAGES ?>lupa.png" />
</div>

Get:

if(isset($_GET['search']))
{   
$pesquisa = $_GET['search'];
$categorySearch = $_GET['category'];
}

It won’t look exactly the way you want it, but it’ll look good!

0

a solution I made is to create a file called search.php that will do this

$termo = $_GET['termo'];
header("Location:".$url_site.'busca/'.$termo);

no . htacess did so

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^busca/([-_a-z0-9]+)/?$ index.php?page=busca&termo=$2 [L]

/*esta segunda tem o parametro parte que é paginação*/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^busca/([-_a-z0-9]+)/parte-([0-9]+)/?$ index.php?page=busca&termo=$2&pag=$3 [L]

Browser other questions tagged

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