slashes in url variables
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
namtax
Updated on November 27, 2020Comments
-
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 almost 14 yearsOk, this seems like an good idea, is there any specif reason to use %2F?
-
SLaks almost 14 yearsThis is the standard URL encoding.
-
Piotr Kula over 10 yearsIt 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 over 10 yearsIIS still intercepts this as a
/
and breaks the route. :( -
chim over 9 yearsApache interprets this as a / and breaks the route unless AllowEncodedSlashes directive is switched on (by default it's switched off)
-
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 over 7 yearsIn 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 findfoo_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 about 7 yearsThe function URLEncoder is not defined in some browsers, e.g. Chrome. So I suggest to use
encodeURIComponent
, w3schools.com/jsref/jsref_encodeuricomponent.asp -
Keavon almost 7 yearsYou can use
encodeURIComponent
anddecodeURIComponent
for this purpose. -
meYnot about 4 yearsI would recommend %5C not %2F
-
cubetronic almost 2 years%5C is a backslash, not a slash