开发者

slashes in url variables

I have set up my coldfusion application to have dynamic urls on the page, such as

www.musicExplained/index.cfm/artist/:Varia开发者_JS百科bleName

However my variable names will sometimes contain slashes, such as

www.musicExplained/index.cfm/artist/GZA/Genius

This is causing a problem, because my application presumes that the slash in the variable name represents a different section of the website, the artists albums. So the URL will fail.

I am wondering if there is anyway to prevent this from happening? Do I need to use a function that replaces slashes in the variable names with another character?


You need to escape the slashes as %2F.


You could easily replace the forward slashes / with something like an underscore _ such as Wikipedia uses for spaces. Replacing special characters with underscores, etc., is common practice.


You need to escape those but don't just replace it by %2F manually. You can use URLEncoder for this.

Eg URLEncoder.encode(url, "UTF-8")

Then you can say

yourUrl = "www.musicExplained/index.cfm/artist/" + URLEncoder.encode(VariableName, "UTF-8")


Check out this w3schools page about "HTML URL Encoding Reference": https://www.w3schools.com/tags/ref_urlencode.asp

for / you would escape with %2F

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜