Categories
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:

/tracks/add/5449326

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:

/overlay/add-track/{param1}/{param2}

{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:

/overlay/add-track/1//tracks/add/5449326

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

Any ideas how to fix this?

PHP

$url = rawurlencode('/tracks/add/5449326');    
echo '<a href="https://www.faqcode4u.com/overlay/add-track/1/".$url.'">Add</a>';

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

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

EDIT

  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]