php – Problem with search and friendly url

Question:

Goodnight,

I have a search that is working fine, I'm just now having a problem when someone writes a word with an accent or two words separated by a space, it breaks my url.

Would you 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

Actual

RewriteRule ^locais/pesquisa/([\p{L}\p{N}\s_-]+)$  index.php?controller=pesquisa&option=pesquisa&pesquisa=$1

Answer:

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

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

Scroll to Top
AllEscort