Problem with search and url friendly

Asked

Viewed 354 times

3

Good night,

I have a search that is working well, I’m only now having a problem when someone writes a word with accent or two words separated by space, breaks my url.

I’d like to know how I can resolve this situation?

Code

 <form method="post" id="form" action="">
          <div style="margin-left:150px;">
              <div style="float:left; width:500px;"><input style="height: 38px;" id="valor_pesquisa" name="valor_pesquisa" type="text" placeholder="Restaurantes, bares, hotéis..." /></div>
              <div style="float:left; margin-left:5px;"><input type="submit" value="Pesquisar" /></div>
          </div> 
        </form>
        <script>
          $(function() {
              $("#form").submit(function(e) {
                  if($('#valor_pesquisa').val() === ''){
                      $("#form").attr("action", "http://sabeonde.pt/locais/pesquisa/todos");
                  }else{
                    var valor_pesquisa = $("#valor_pesquisa").val();
                    $("#form").attr("action", "http://sabeonde.pt/locais/pesquisa/"+valor_pesquisa);
                  }
              });
          });
        </script>

.htaccess

# Rota Pesquisa
RewriteRule ^locais/pesquisa/([a-zA-Z-0-9-_]+)$  index.php?controller=pesquisa&option=pesquisa&pesquisa=$1

Current

RewriteRule ^locais/pesquisa/([\p{L}\p{N}\s_-]+)$  index.php?controller=pesquisa&option=pesquisa&pesquisa=$1
  • From the Ternal server error if you take out the error you have in regex you already open the site if you have the space of the Ternal server error

  • Works the spaces but accents not put up in the current as this

3 answers

1

Well I ended up solving my problem with the following regex so that you need.

RewriteRule ^locais/pesquisa/([A-Za-záàâãéèêíïóôõöúçñÁÀÂÃÉÈÊÍÏÓÒÖÚÇÑ\s_-]+$)$  index.php?controller=pesquisa&option=pesquisa&pesquisa=$1

Of course with the help of all the answers given here thanks to all

  • and if the search contains numbers?

0

I believe you can include other ranges in Regex to allow accented characters, like [À-üa-zA-Z0-9 _-]+

  • With this already accepts the accents but spaces not

-1

var valor_pesquisa = window.encodeURIComponent($("#valor_pesquisa").val());

  • tested this way but breaks the same url returns to the error page

Browser other questions tagged

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