2

On OSX when I try to open a url that contains a hash #, it gets converted to %23 and it fails. For example,

From the command prompt,

$ open "https://translate.google.com/#auto/en/ру́сский+язы́к"

gives me an Error 404 from google

The requested URL /%23auto/en/%D1%80%D1%83%CC%81%D1%81%D1%81%D0%BA%D0%B8%D0%B9+%D1%8F%D0%B7%D1%8B%CC%81%D0%BA was not found on this server. That’s all we know.

The same happens from Applescript as well when I use the open location command.

Is this happening at my end or from googles? How can I get these urls to work? According to this answer it should work.


EDIT:

I disabled my internet connection and then tried to open the above url and the results was the same, # was turned into %23. Also tried Chrome, I was using Firefox, and the result is the same. So the change is happening by OSX itself.

By the way, if you copy and paste the above into your browser it will work just fine, so the url is fine as is.

Lastly, I am using Snow Leopard (10.6.8)

SOLVED: As per the answer below, the query string had to be encoded. The one liner in this answer helped,

/bin/echo -n "some foreign text" | php -r "echo urlencode(file_get_contents('php://stdin'));"

2 Answers 2

1

You wrote "Russian + language".

The translation in Russian uses special characters. Special characters in the URL are being coded as "%??" equivalents as a reference for these characters.

I looked this up for you so you can see: http://www.w3schools.com/tags/ref_urlencode.asp.

The URL Does not know what a special character is. Are you programming? If so you'll have to find an equivelent way to transcode your characters into somethig the URL can recognize.

Here is an online decoder for further investigation relevant to special characters: http://www.asiteaboutnothing.net/c_decode-url.html

2
  • When I saw google's response I copied and pasted its encoded string but it still failed so I didn't look at that again. After checking the w3schools link in your answer I tried again and it worked! So now with the help of a php one liner the links work. thx. Dec 29, 2015 at 22:30
  • Glad I could help. I thought it might be php, I ran into the same problem a couple years ago.
    – ejbytes
    Dec 30, 2015 at 22:24
1

Essentially the problem is that open is not a web browser, and cannot distinguish between two special cases as it passes the information to a browser:

  • the #auto (URL fragment) should be passed as-is
  • the other text has to be URL-encoded to accommodate "older" browsers.

Different browsers may handle these URLs differently on the command-line versus using their interface. If you open the given URL on the command-line directly invoking Firefox, it works as expected.

Further reading:

1
  • I realise that open is not a browser. What confused me is that it passed the string correctly, or it seemed to so I didn't look at the encoding part too much. As for the browser being old, I am using the latest Chrome and Firefox so there is no issue there. Your answer was helpful though in that it helped me focus on the encoding of the text string and ignore the #. Thx. Dec 29, 2015 at 22:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .