1
  • I have a shortened URL from some documentation
  • I would like to open the URL because I believe the URL has good information
  • but I do not want to click the link until I can see the full URL

how to get full URL from shortened URLs like bit.ly?

3 Answers 3

1

For Firefox:

For Chrome based browsers:

I've not tested any.

Curl: curl -s -I "$URL" | awk '/location:/{print $2}'

wget probably can do this as well (it has -d) but I'm half-asleep right now to read its manual.

1
  • See my comment on #736239 Commented May 16 at 22:44
0

I found a simple elegant solution using shell command:

curl --head bitly.is/urlshortening|grep Location|awk '{print $2}'

explanation of the shell command:

  • curl with --head gets only the HTTP header
  • grep gets you the line of text with URL value
  • awk prints you just the URL value
  • done

even shorter: curl --head --location bitly.is/urlshortening

The reason why using curl works is because the URL shortener is doing an HTTP redirect and so this solution uses the knowledge about how HTTP redirects work.

p.s. IMO curl is significantly better than a browser extension or third-party website because the curl solution is (1.) faster IMO than having to vet N browser-extensions/third-party-websites (2.) and the shell command is easy even for beginners (3.) browser-extensions/third-party-websites come and go.

5
  • 1
    you can also actually extract that header field using curl. No need to manually look for the Location field. add a -L to your command line Commented Feb 20, 2023 at 18:50
  • 2
    @MarcusMüller that triggers a HEAD on the target URL, which may have undesirable side-effects. Extracting the Location field from the bit.ly HEAD avoids that Commented Feb 20, 2023 at 22:05
  • @MarcusMüller thanks i'll add that. Commented Feb 21, 2023 at 18:43
  • 1
    -L or --location actually performs the redirect, it does not "extract" what's in the Location field Commented May 2 at 13:40
  • HTTP header names are conventionally camelcase but officially are case-insensitive and some servers vary; grep -i ^location: would be more robust. Or with gawk something like BEGIN{IGNORECASE=1} $1~/location/{print $2} Commented May 16 at 22:43
0

Using GNU AWK (HTTP only):

echo 'http://bitly.is/urlshortening' |
  awk 'BEGIN{
        ORS="\r\n"   # end printed lines with CRLF
        IGNORECASE=1 # header names vary in capitalization
       }
  (gsub(/^http:\/\//,"")==0){ print "unable to use " $0; next }                                                                                                   
  { domain = gensub(/\/.*$/,"","g")   # e.g. bitly.is
    path = gensub(/^[^/]*\//,"/","g") # e.g. /urlshortening
    http_connection = "/inet/tcp/0/" domain "/80"
    print "HEAD " path " HTTP/1.1" |& http_connection
    print "Host: " domain |& http_connection
    print "Connection: close\n" |& http_connection
    while ((http_connection |& getline) > 0)
      if(gsub(/^Location: /,"") > 0)
        print
  }'

This submits a HEAD request. I got the idea from this question on CodeGolf: https://codegolf.stackexchange.com/questions/61836/the-worlds-smallest-web-browser

This only works for link shortening services that support just HTTP (but I checked, and both bitly.is and tinyurl allow it!). There's probably a way to do HTTPS too (use /443 and...encrypt it somehow), but my knowledge doesn't go that far.

2
  • 1
    Although many webservers will Postelianly accept that, the lines of an HTTP request header (or response header also) should end with CRLF not just LF, and there should be only one empty line following the header not two. Also header names may vary in capitalization, which gawk can handle easily with IGNORECASE=1 . Commented May 16 at 22:45
  • Thanks, didn't know that! Added both changes. Commented May 16 at 23:56

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.