apache javascript php url-encoding urlencode

Proper way to encode a (relative) URL using PHP

I’m trying to encode the following relative URL using PHP:


I’ve tried using rawurlencode and urlencode.

Which made it look something like: %2Ftracks%2Fadd%2F5449326

I’m going to use this URL in an AJAX call.

The URL I am trying to make the call to is:


{param1} is just some id

{param2} is the encoded url

However when I try to make the call I get a 404.

From the looks of it, it is trying to make a call to:


So it looks like it’s trying to access the decoded string.

Any ideas how to fix this?


$url = rawurlencode('/tracks/add/5449326');    
echo '<a href="".$url.'">Add</a>';

JS (jQuery)
var href = $(‘a’).attr(‘href’);

url: href,
type: 'POST',
data: {},
dataType: "json",
success: function(data)
// do stuff


  RewriteEngine on
RewriteCond %{REQUEST_FILENAME} \.(eps|js|ico|gif|jpg|png|css|jpeg|doc|xls|doc|pdf|txt|ppt|zip)$
RewriteRule ^(.*)$ $1 [L]
RewriteCond "/path/public/%{REQUEST_URI}" !-f
RewriteRule ^(.*)$ /index.php/$1 [L]