Search code examples
vbscriptasp-classicurlencode

Server.URLEncode started to replace blank with plus ("+") instead of percent-20 ("%20")


Given this piece of code:

<%
    Response.Write Server.URLEncode("a doc file.asp")
%>

It output this for a while (like Javascript call encodeURI):

a%20doc%20file.asp

Now, for unknow reason, I get:

a+doc+file%2Easp

I'm not sure of what I touched to make this happen (maybe the file content encoding ANSI/UTF-8). Why did this happen and how can I get the first behavior of Server.URLEncode, ie using a percent encoding?


Solution

  • Classic ASP hasn't been updated in nearly 20 years, so Server.URLEncode still uses the RFC-1866 standard, which specifies spaces be encoded as + symbols (which is a hangover from an old application/x-www-form-urlencoded media type), you must be mistaken in thinking it was encoding spaces as %20 at some point, not unless there's an IIS setting you can change that I'm unaware of.

    More modern languages use the RFC-3986 standard for encoding URLs, which is why Javascript's encodeURI function returns spaces encoded as %20.

    Both + and %20 should be treated exactly the same when decoded by any browser thanks to RFC backwards compatibility, but it's generally considered best to use %20 when encoding spaces in a URL as it's the more modern standard now, and some decoding functions (such as Javascript's decodeURIComponent) won't recognise + symbols as spaces and will fail to properly decode URLs that use them over %20.

    You can always use a custom function to encode spaces as %20:

    function URL_encode(ByVal url)
        
        url = Server.URLEncode(url)
        url = replace(url,"+","%20")
        
        URL_encode = url
        
    end function