slashes in url variables

UrlEscaping

Url Problem Overview


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

www.musicExplained/index.cfm/artist/:VariableName

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?

Url Solutions


Solution 1 - Url

You need to escape the slashes as %2F.

Solution 2 - Url

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.

Solution 3 - Url

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")

Solution 4 - Url

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

for / you would escape with %2F

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnamtaxView Question on Stackoverflow
Solution 1 - UrlSLaksView Answer on Stackoverflow
Solution 2 - UrlCaleb HearthView Answer on Stackoverflow
Solution 3 - UrlAniket ThakurView Answer on Stackoverflow
Solution 4 - UrlInquisitusView Answer on Stackoverflow