The correct way to encode a (relative) URL using PHP

I am trying to encode the following relative url using PHP:

/tracks/add/5449326 

I tried using rawurlencode and urlencode.

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

I am going to use this url in an AJAX call.

The url I'm trying to do is:

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

{param1} is just some id

{param2} - encoded URL

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

In appearance, he is trying to call:

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

So it looks like it is trying to access the decoded string.

Any ideas how to fix this?

Php

 $url = rawurlencode('/tracks/add/5449326'); echo '<a href="/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] 
+4
source share
1 answer

The encoded slash is probably not allowed by the web server, resulting in a default of 404.

For apache see:

http://httpd.apache.org/docs/current/mod/core.html#allowencodedslashes

Any apache configuration needs to be changed or you need to get around it by replacing the slash / backslash with something else.

edit: You can try double urlencode and then decodeURI () in javascript.

0
source

All Articles