skip to Main Content

In my web page search input I am using the get method, and the URL is something like this www.example.com/search?city=example&service=example2, but I want a URL like this www.example.com/search/example/example2; how can I convert?

Or maybe there is some other solution to get the data from html inputs and get a friendly URL?

3

Answers


  1. Chosen as BEST ANSWER

    I see others posting about routing, yes i can do like what, but i think first i need to solve problem with html submit action, because when i press the button its auto redirect to what standart format url search?city=example&service=example2, but like i say i need other format. So how i can solve this problem?

    My form action

    <form method="get" action="http://example.com/search">
    

  2. In your routes.php file inside the config folder:

    $route['search/(:any)'] = 'search/searchFunction'; //You will now need to have a searchFunction() inside the controller search. Or you can just have 'search' and handle the code below inside the index() function.

    Now, inside your searchFunction:

    function searchFunction()
    {
      $city = (isset($this->uri->segment(2))) ? $this->uri->segment(2) : null; //if you have www.example.com/search/example
      
      $service = (isset($this->uri->segment(3))) ? $this->uri->segment(3) : null; //if you have www.example.com/search/example/example2
      
    //General Pattern is: www.example.com/uri-segment-1/uri-segment-2/uri-segment-3.....and so on.
    
      // Now when you are querying, if($city) then 'WHERE city = $city' and so on.
      
      //Also, in the link that was earlier triggering 'www.example.com/search?city=example&service=example2'
      //like <a href='www.example.com/search?city=example&service=example2'>Link</a>
      //Now have it as:
      // <a href='www.example.com/search/example/example2'>Link</a>
      
      
    }
    Login or Signup to reply.
  3. I think its help you and also SEO.
    Please write following uri rules in : application/config/routes.php

    $route['search-(:any)-(:any).html'] = 'search?city=$1&service=$2';
    

    its rewrite your url from www.example.com/search?city=example&service=example2 to www.example.com/search-example-example2.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search