Make your own URL shortener in PHP - Part 2

Here is the part -2 of our URL shortener making tutorial. 

In the first part of this tutorial:

  • We already made a table to store long and short URLs with an auto increment id column used to generate the short URL.
  • We made an HTML form on one page which takes long / original URL from the user and submits it to a PHP scripted page.
  • That PHP scripted page handles the long URL by inserting it in the database first and then generates the short URL.

If you missed the first part of this tutorial: Click Here. 

You can also check the live implementation here: tx01.comeze.com/urlS

        In this part I am going to show you how to capture the short URL on our server and redirect it to a script in order to search the short URL in our database and get its original URL from there and make a redirect to it. So let’s get started.


Capturing the URL on our request to the server:

        If you have looked at our implementation links than we have our URL shortening part on urlS directory and we are giving short links of urlR directory. That means we have made two directories on our server. One is urlS and another is urlR. Many URL shortening service use two different domains to do such implementations or some others use shortening service inside of some directory and redirecting service in main directory. In a nutshell it is advised to have these to implementation separate.

        Now we will use Apache server’s .htaccess code lines also known as Directives to get the information about request made in our urlR directory on the server. You can find the whole documentation about .htaccess directives on apache’s website. 
Flow:
  • In our urlR directory we have a redirect.php script which gets the long url from database using the short URL which we use to send the request. And a .htaccess file
    • If you are new to this file name I would like to tell you that this is itself a filename “.htaccess” and you don’t have to give a separate filename to this file
    • We also have a invalid_url.php in case the URL requested is invalid. 
    • As show in the first part of this tutorial we generated a short link and just concatenated it to the link on which we have written the script to handle that short URL and resolve it.
    • For e.g If short URL generated is abcXYZ  then we have our URL like: http://tx01.comeze/urlR/abcXYZ
    • And as you can see here we have given the link of urlR directory on our website tx01.comeze.com to handle this short URL because that’s where we have made the handling script.
  • Here is the full code of .htaccess file
1 
2 
3 
4 
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^([A-Za-z0-9-_]+)$ redirect.php?id=$1 [L,QSA]
</IfModule>



  • Let me explain you this code with an example:
    • For e.g.: if you request for - http://tx01.comeze/urlR/abcXYZ 
    • Than according to the third line i.e RewriteRule ^….. 
    • We are checking if the URL consist of words from the set { A to Z OR a to Z OR 0-9 OR and underscore ( _ ) or and hyphen ( - ) } just because when we made our short URL it consisted only these 64 characters.
    • Hence any request, not containing these letters, must be handled by server itself and we don’t have anything to do with that.
    • So if the comparison comes true we redirect our request to redirect.php?id=$1 Where $1 is last part of our short URL which is fetched from the URL. 
    • The last part of code line i.e [L,QSA] are called Rewrite rule flags:
      • L is called Last which causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.
      • Using the [QSA] flag causes the query strings to be combined. Here our query string is the short URL which comes after urlR/ part.
    • You can get more information about L and QSA here: https://httpd.apache.org/docs/current/rewrite/flags.html
In the next part, which will be the last one, I will show you the short url handling and redirection code written in redirect.php. 

If you have any query regarding this article you can surely mention in the comment section, I will try to reply as soon as possible.

Keep reading, keep practicing. Happy coding!

Comments

Popular posts from this blog

Windows phone Wifi connectivity problem.

Create Excel file using Java: Apache POI - PART - 2: Line Chart