You can spin up a web server on your laptop by typing the following command in your terminal.
python -m CGIHTTPServer
That is it!
You can test it out by creating an html file in the same folder called index.html and accessing it through browser by typing
Let us say the contents of index.html was:
<html> <body> <p>Hello DEI Team</p> </body></html>
Then, you will see the following in your browser
Hello DEI Team
The purpose of this article is to see how to redirect one url or website to another. We will be discussing only one of many possible methods.
Let us say that whenever someone accesses redirect.html, you want them to go to google.com
Type the following in the redirect.html
<meta http-equiv=”refresh” content=”0; URL=’http://google.com'” />
<p>Hello DEI Team, We are redirecting you to google.com</p>
The key is the ‘meta’ statement.
Now, if you point the browser to
You will be redirected to Google!
Let us make one tiny modification to how we launched the webserver. Let us now run the following command instead.
- nohup python -m CGIHTTPServer >foo.out 2>foo.err&
The core command is still the same. We are now capturing the output in foo.out and the debug/error messages in foo.err. We are also running the program forever in background by using nohup and ‘&’, so that we can close the terminal without closing the server.
Now, whenever you access a website – be it index.html or redirect.html, you will see the actual URL that was accessed in foo.err.
127.0.0.1 – – [19/Apr/2018 09:40:50] “GET / HTTP/1.1” 200 –
127.0.0.1 – – [19/Apr/2018 09:43:30] “GET /redirect.html HTTP/1.1” 200 –
I deliberately started this post with a controversial approach – I started with details rather than motivation.
The reason is, the details were simple and absolutely delightful! I mean, the ability to start a webserver on my laptop or any host in under a minute is just mindblowing!
Now let us consider WHY the Marketing industry might be interested in redirects.
- Link Shortening – Consider bit.ly
- Ad Links – first the ad server records the click, before redirecting to the promised website
- Detecting clicks on emails (same as above, just a different channel)
- Dynamically redirecting to Google Play or Apple App Store based on your device
Let me now answer the obvious question – suppose we were to send a million emails, and each email had a unique link, how could I do that? Will I need 1 million web pages?
Of course not! We need CGI scripts! Create a folder called cgi-bin and create a file called hello.py in that folder with following contents
localvars_table = ‘<table>’
for x in dir():
localvars_table += ‘<tr><td>%s</td></tr>’ % x
localvars_table += ‘</table>’
<p>Hello World! Your custom CGI script is working. Here are your current Python local variables.</p>
<p>NOTE: If you want to write useful CGI script, try the Python ‘cgi’ module. See cgitest.py script.</p>
</body></html>””” % (localvars_table))
Now, you can access it with the following url in the browser
And, you could also access the same, without any problems with
The only difference will be what was recorded in the logs (in foo.err)
127.0.0.1 – – [19/Apr/2018 10:12:51] “GET /cgi-bin/hello.py HTTP/1.1” 200 –
127.0.0.1 – – [19/Apr/2018 10:18:45] “GET /cgi-bin/hello.py?user_id=srib HTTP/1.1” 200 –
In this short post, we have seen how link redirection *really* works and appreciate the technology behind Ad Clicks, Email tracking, URL shortening and Dynamic URLs.