开发者

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 urlencod开发者_StackOverflow社区e.

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="/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]


The encoded slash is probably not allowed by the webserver resulting in a default 404.

For apache see:

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

Either the apache config needs to be changed or you need to work around it by replacing the slash/backslash with something else.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜