Ad

How To Convert A Link To Seo Friendly Url In Jquery

- 1 answer

I am using jquery to send the data to another page. But the url is not look good, so I want to make it as SEO friendly URL.

Below is my script:

function set_item(item) {
    // change input value
    $('#searchitem').val(item);
    // hide proposition list
    $('#search_list_id').hide();

     var location = $('#search_location').val().split(',');
     var search_term = $('#searchitem').val();
     var query =encodeURIComponent(search_term);

     if(search_term != '' && location !=''){
     window.location.href = 'search.php?location=' + location[0] + '&search_term='+ query;
     }
}

Now my url is showing like:

http://www.zesteve.com/search.php?location=Hyderabad&search_term=Traditions%20Events%20Management%20%26%20Marketing%20Pvt%20Ltd

But i want to like below:

http://www.zesteve.com/search/Hyderabad/Traditions-Events-Management-&-Marketing-Pvt-Ltd

I have no idea do I required to use .htaccess or any additional jquery?

Ad

Answer

You might do something like this:

.htaccess:

RewriteEngine on
RewriteRule ^search/(.+)$ /search.php?path=$1 [NC,L]

search.php:

<?php
if(isset($_GET['path']))
{
    $pathParts = explode('/', $_GET['path']);

    var_dump($pathParts);

    echo "<br /><b>Location:</b> " . $pathParts[1];
    echo "<br /><b>Search Term:</b> " . $pathParts[2];
}   
?>

Docs for .HTAccess
https://httpd.apache.org/docs/current/howto/htaccess.html

PHP Explode Function
http://php.net/manual/pt_BR/function.explode.php

Ad
source: stackoverflow.com
Ad