slashes in url variables

215,248

Solution 1

You need to escape the slashes as %2F.

Solution 2

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

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

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

for / you would escape with %2F

Share:
215,248
namtax
Author by

namtax

Updated on November 27, 2020

Comments

  • namtax
    namtax over 3 years

    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?

  • namtax
    namtax almost 14 years
    Ok, this seems like an good idea, is there any specif reason to use %2F?
  • SLaks
    SLaks almost 14 years
    This is the standard URL encoding.
  • Piotr Kula
    Piotr Kula over 10 years
    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
    Piotr Kula over 10 years
    IIS still intercepts this as a / and breaks the route. :(
  • chim
    chim over 9 years
    Apache interprets this as a / and breaks the route unless AllowEncodedSlashes directive is switched on (by default it's switched off)
  • vsync
    vsync almost 8 years
    @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
  • William Isted
    William Isted over 7 years
    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.
  • Admin
    Admin about 7 years
    The function URLEncoder is not defined in some browsers, e.g. Chrome. So I suggest to use encodeURIComponent, w3schools.com/jsref/jsref_encodeuricomponent.asp
  • Keavon
    Keavon almost 7 years
    You can use encodeURIComponent and decodeURIComponent for this purpose.
  • meYnot
    meYnot about 4 years
    I would recommend %5C not %2F
  • cubetronic
    cubetronic almost 2 years
    %5C is a backslash, not a slash