Simone Simone - 2 months ago 6
Ajax Question

.htaccess, how can I remove pattern from url?

I would like to clean my urls from this:

https://example.com/track&id=180


to:

https://example.com/track/180


My code is as follow:

My
.htaccess


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^welcome/([^/]+)/?$ index.php?a=welcome&filter=$1 [NC,QSA,L]
RewriteRule ^track/([^/]+)/?$ index.php?a=track&filter=$1 [NC,QSA,L]
RewriteRule ^(([^/]+)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L,QSA]


But if I use the 2nd example (the url without
&id=
) via Ajax it returns "undefined".

I tried the Ajax call with this link button

<a href="'.$this->url.'/track/'.$id.'" rel="loadpage">Click Here</a>


I supposed it's undefined because in my requests I get the
id
parameter in PHP as
$_GET['id'];
.

So my question is: why it doesn't work on Ajax?

Here is my
functions.js


$(function() {
$("body").on("click", "a[rel='loadpage']", function(e) {
// Get the link location that was clicked
startLoadingBar();
pageurl = $(this).attr('href');
// Replace the path of the url from index to the path with raw content
var custom = pageurl;
var custom = custom.replace(baseUrl+'/track', 'page.php?a=track');
var custom = custom.replace(baseUrl+'/', 'page.php?a=welcome');
// Request the page
$.ajax({url:custom,success: function(data) {
// Show the content
$('#content').html(data);
// Stop the loading bar
stopLoadingBar();
// Scroll the document at the top of the page
$(document).scrollTop(0);
// Reload functions
selectExplore();
reload();
}});


// Store the url to the last page accessed
if(pageurl != window.location){
window.history.pushState({path:pageurl}, '', pageurl);
}
return false;
});
});


.htaccess
isn't my forte, so any help would be much appreciated.

Thanks in advance.

Answer

You can use this JS code to send your AJAX call:

var custom = pageurl.replace(baseUrl+'/track/', '/page.php?a=track&id=');

// Request the page
$.ajax({url:custom,success: function(data) {
   // ...
}