155

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?

0

4 Answers 4

253

You need to escape the slashes as %2F.

7
  • 3
    This is the standard URL encoding.
    – SLaks
    Jun 7, 2010 at 19:03
  • 59
    IIS still intercepts this as a / and breaks the route. :(
    – Piotr Kula
    Jan 10, 2014 at 12:50
  • 31
    Apache interprets this as a / and breaks the route unless AllowEncodedSlashes directive is switched on (by default it's switched off)
    – chim
    Nov 7, 2014 at 10:41
  • 7
    You can use encodeURIComponent and decodeURIComponent for this purpose.
    – Keavon
    Jun 26, 2017 at 19:17
  • 2
    %5C is a backslash, not a slash
    – cubetronic
    May 19, 2022 at 19:35
17

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.

3
  • 5
    It is common practise but it is NOT best practise. Using escaped characters is best practise since every browsers understands this, every server understands this and every developer should learn to do it this way. UNderscores ARE BAD FOR SEO also! I am just saying this as I used to do this also and learnt the hard way it comes back and stings you hard.
    – Piotr Kula
    Jan 10, 2014 at 10:57
  • 1
    @ppumkin - why do you think so? using escaped characters is not really a best practice since it produces URLS which are not user-friendly and might looks very weird to non-tech users. I think it's best to try keeping URLs as sensible as possible
    – vsync
    May 23, 2016 at 11:59
  • In regards to the UNderscores ARE BAD FOR SEO comment. Underscores are interpreted as underscores by Google, Dashes / Hyphens are interpreted as spaces. Why? Coders, a lot of coders use Google (including Google themselves since the early days), if they treated underscores as spaces you would no longer be able to find foo_bar (likely a class of some kind) within the search results. Blah blah... In conclusion: Underscores are not bad for SEO if you understand how the search engine you're "optimising" for actually works. Aug 23, 2016 at 13:24
17

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

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

for / you would escape with %2F

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.