Best URL Shortener: Polr

Best URL Shortener: Polr

13.Sep.2021

Do you want to create your own custom URL shortener? Maybe you want an easy way to access your Bit.ly links through a simple script that will work with whichever domain name you choose. If this sounds like something that would be useful for your website, then read on.

First, the server space must be prepared with the appropriate software in order for Polr to work properly. Assuming you're using a Linux machine, most of what needs to be installed is typically already available by default in most cases. However, there are some PHP modules that are not included by default which are required before installing Polr - specifically cURL and JSON . For more information about each of these modules visit their respective pages on php.net .

Also, you must first create the required directory structure where Polr will be stored.

To begin, create a new directory named polr inside of your server's document root (e.g., /var/www). Then from within this folder, make another new directory named html . Within this html directory is where all of the scripts that comprise Polr will reside. For example: <SERVER ROOT>/pol/.htaccess

<SERVER ROOT>/html/index.php

<SERVER ROOT>/html/config.php*

*if you want to use any additional config options for your short URLs, it is suggested to create a copy of the default config file and edit as needed.

Now that the server space has been prepared, install Polr according to these instructions .

When you have finished installing and configuring Polr, go to your domain name via a web browser. If everything was installed correctly, you should see the following page:

Click on 'Submit short link'. Here you will be prompted for your API key - this is found within your account settings at bit.ly , goo.gl , or any other service that uses the bit.ly API (we'll discuss this later). Enter in your API key and click 'Go'. You should then see something along these lines: http://localhost/pol/ + whatever text you entered into the input box (e.g., polr) = http://localhost/pol/p*r

 

Now, any time you visit http://localhost/pol/p*r (or whatever URL was generated), it will redirect to its previous location (e.g., your website).

If you want to use a custom domain name for your short URLs, we'll need to cover how the bit.ly API works and configure Polr accordingly. If you don't, skip this section and read on about installing the shell script .

First off, bit.ly requires an application name - I've used 'Pol' here but anything is up to you. It also requires an API key so go ahead and register for one if you don't have one already , or log in with your Bitly account you already have one registered.

Go to your account settings and make a new application.

Once you have created this application, you will see the API ID and API key for it - these will be needed later on when configuring Polr.

Log into bitly/shrink with your existing credentials (or create a new one), then click on 'Create'. Enter in whatever name you want as well as your API key from above into the relevant fields, then hit submit. You should now see something like the following: http://localhost/pol/1255225485-7i5kDO1V8oBHGqSgOw4nbqt0mVdSBJJLBSaYsEQh5Uzlhdzg.BcS5WQmf3vbl

Create a new directory under the Polr root directory called bitly and within this new folder make another one called shrinks . In here will be where we keep our shell script used with the bit.ly API to shorten URLs in bulk - you can choose whatever name you prefer instead of 'shrink' though, it's up to you.

Once in this directory, create a file named 'polr_bitly_shink_script.sh'. This is where we'll fill in some information about our short URL application that was created earlier on when using the Bit.ly UI directly (the same application ID and key above). Enter in these two pieces of information accordingly:

#!/bin/bash     APIID="<your application ID here>"     APIPASS="<your API key here>"

bitly_create() {    curl -F "shortUrl=true" -d "{ \"url\": \"$1\", \"login\":\"$2\", \"format\":\"text\" }" https://api-ssl.bitly.com/v3/shrink     }

Then, before you forget, make sure the bitly_create function is executable: chmod +x polr_bitly_shink_script.sh

Now we'll create a shell script that will call our newly created Bit.ly API URL and pass it the short link as input along with our login credentials.

Create another file in this directory called 'polr_shorten'. Make sure it has the following code:

  "polr"  |    curl -sS --connect-timeout 60 -F login=<your account email> -F apiKey="$APIPASS" https://api-ssl.bitly.com/v3/shrink?login=$2 | grep ID | cut -d'=' -f2 | cut -d'/' -f1

This shell script will call the Bit.ly API URL that was generated when you made your application (the one that starts with 'http://localhost/pol') and pass it two variables, your Polr login credentials ( <your account email> ) and your API key. We need to make this file executable too: chmod +x polr_shorten

The next step is to create a cron job that will run daily, or more often depending on how much you plan on using it. Crontab is the Linux tool for running certain commands/shell scripts at predetermined intervals. Within this file, enter in the following - just be sure to replace 'username' with your username:     0 0 * * * /home/username/pol/shrink > /dev/null 2>&1

This code tells your computer to run our Polr shell script every day at 12 AM (midnight) - any output from this command will be sent to /dev/null. Make sure the file 'polr_shrink' that we created earlier is executable: chmod +x polr_shorten

Now that all our necessary files are in place, you're good to go! Go ahead and try a short link - just remember that it might take a while for it to appear after being created depending on the time of day.

# BEST URL SHORTENER -- POLR

               USERNAME="username"                  APPLICATIONID="applicationid"                    APIKEY="apikey"           SHORTHANDEDURL="http://example.com/"

                LONGHANDEDURL="http://example.com/"                 NEWSHORTHANDEDURL="http://localhost/pol/$1"                    bitly_create() {                     curl -F "shortUrl=true" -d "{ \"url\": \"[email protected]\", \"login\":\"$2\", \"format\":\"text\" }" https://api-ssl.bitly.com/v3/shrink         }         polr_shorten() {                 bitly_create "[email protected]" && echo "$NEWSHORTHANDEDURL" | cut -d'/' -f1 && echo "$LGHANDEDURL" | cut -d'/' -f2 }         polr_bitly_shink_script.sh                 #!/bin/bash  APIID="$APPLICATIONID"  APIPASS="$APIKEY"

if [ "$#" -eq 0 ]         then                   POLRSHORTENEDURL=""                   else                   POLRSHORTENEDURL=$(polr_shorten "[email protected]")     fi

polr_create() {    curl https://api-ssl.polr.name/v1/polr/$USERNAME/$POLRSHORTENEDURL | head -n 1 }

export USERNAME=username export APPLICATIONID=applicationid export APIKEY=apikey polr_create "[email protected]"

This code will be run every time you invoke the shell script. The first part of the code (starting with ' POLRSHORTENEDURL="" ') is a small wrapper that captures any variables passed to the shell script and stores them into proper variables, as shown below:

USERNAME="username" APPLICATIONID="applicationid" APIKEY="apikey" SHORTHANDEDURL="http://example.com/"    LONGHANDEDURL="http://example.com/" NEWSHORTHANDEDURL="http://localhost/pol/$1" bitly_create "[email protected]" && echo "$NEWSHORTHANDEDURL" | cut -d'/' -f1 && echo "$LGHANDEDURL" | cut -d'/' -f2

POLRSHORTENEDURL="" polr_shorten "[email protected]"

The second part of the code (starting with ' POLRSHORTENEDURL=$(polr_shorten "[email protected]") ') is the actual command that will be run every time this shell script is invoked. It first passes all command-line parameters to the shortening function, and then stores any output from that into a variable called ' POLRSHORTENEDLINE '. Finally, we invoke the creation function and pass it our shortened link as well as any other variables we used throughout the script.

This brings us to our final function, which is going to be invoked through cron. The first part of this code (starting with ' bitly_create "[email protected]"') is a wrapper for the shell commands necessary to create a link on Bitly's website, and store it into a variable called 'NEWSHORTHANDEDURL'. The second half handles retrieving the newly-created link from Bitly and storing it in a variable called ' LONGHANDEDURL '.

export USERNAME=username export APPLICATIONID=applicationid export APIKEY=apikey polr_create "[email protected]"

 

We are social