Asesha George Asesha George - 7 months ago 9
Javascript Question

how to convert a link to seo friendly url in jquery

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 shwoing 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?

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

Comments