<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>phoenixheart - portfolio &#38; more &#187; url shorten</title> <atom:link href="http://www.phoenixheart.net/tag/url-shorten/feed/" rel="self" type="application/rss+xml" /><link>http://www.phoenixheart.net</link> <description>phoenixheart - portfolio &#38; more</description> <lastBuildDate>Wed, 25 Aug 2010 03:02:03 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <script type="text/javascript">/*<![CDATA[*/if(typeof Meebo=="undefined"){Meebo=function(){(Meebo._=Meebo._||[]).push(arguments)};(function(q){var args=arguments;if(!document.body){return setTimeout(function(){args.callee.apply(this,args)},100);}var d=document,b=d.body,m=b.insertBefore(d.createElement('div'),b.firstChild);s=d.createElement('script');m.id='meebo';m.style.display='none';m.innerHTML='<iframe id="meebo-iframe"></iframe>';s.src='http'+(q.https?'s':'')+'://'+(q.stage?'stage-':'')+'cim.meebo.com/cim/cim.php?network='+q.network;b.insertBefore(s,b.firstChild);})({network:'phoenixheartnet_bo16we'});}/*]]>*/</script> <item><title>Write your own URL shortener</title><link>http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/</link> <comments>http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/#comments</comments> <pubDate>Thu, 04 Jun 2009 11:53:37 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[twitter]]></category> <category><![CDATA[url shorten]]></category><guid
isPermaLink="false">http://www.phoenixheart.net/?p=305</guid> <description><![CDATA[With Twitter and its 140 characters limit leading all the trends, URL shortening services are in extremely high demand. We have seen TinyURL in the past, now Bit.ly, Is.gd, Tr.im and dozens of others are joining the party to share the cake. Honestly saying, to me they&#8217;re all the same (except for ➡.ws for its [...]]]></description> <content:encoded><![CDATA[<p>With Twitter and its 140 characters limit leading all the trends, URL shortening services are in extremely high demand. We have seen <a
href="http://tinyurl.com">TinyURL</a> in the past, now <a
href="http://bit.ly">Bit.ly</a>, <a
href="http://is.gd">Is.gd</a>, <a
href="http://tr.im">Tr.im</a> and dozens of others are joining the party to share the cake. Honestly saying, to me they&#8217;re all the same (except for <a
href="http://➡.ws">➡.ws</a> for its cool name). With that being said however, it is interesting how they are doing it out there &#8211; what mechanism / algorithm / buzzword-here?</p><p>I curiously did some &#8220;research&#8221;es (well, another buzzword), and it seems in order to create the shorten URLs they are following the same steps:</p><ol><li>Insert the original (long) URL into the database</li><li>Get the insert ID. If the URL already exists, take its row ID. Let&#8217;s say we got 123456.</li><li>Convert the ID 123456 into something even shorter, let&#8217;s say &#8220;am4k&#8221;</li><li>Make use of Apache&#8217;s mod_rewrite so that any request to <em>http://host.com/am4k</em> will reach <em>http://host.com/redir_script.php?code=am4k</em> instead.</li><li>In redir_script.php the value &#8220;am4k&#8221; is converted back into base10&#8242;s 123456 and the corresponding URL is queried back from database</li><li>If a URL is found, redirect the request to it.</li></ol><p>It is simple enough&#8230; except for step 3 and 4. Which conversion is there, and how is the .htaccess written? <span
id="more-305"></span></p><p>So I did some other <em>buzzword-here</em>, and it turned out <a
href="http://en.wikipedia.org/wiki/Base36">base36 encoding</a> is taking place. According to Wikipedia, &#8220;the choice of 36 is convenient in that the digits can be represented using the <a
class="mw-redirect" title="Hindu-Arabic numerals" href="http://en.wikipedia.org/wiki/Hindu-Arabic_numerals">Arabic numerals</a> 0-9 and the <a
title="Latin alphabet" href="http://en.wikipedia.org/wiki/Latin_alphabet">Latin letters</a> A-Z&#8221;. There is a handy conversion table too:</p><table
style="border:1px solid #ccc"><tbody><tr
align="right"><th>Decimal</th><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td><td>16</td><td>17</td></tr><tr
align="right"><th>Base 36</th><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td><td>G</td><td>H</td></tr><tr><td
colspan="19"></td></tr><tr
align="right"><th>Decimal</th><td>18</td><td>19</td><td>20</td><td>21</td><td>22</td><td>23</td><td>24</td><td>25</td><td>26</td><td>27</td><td>28</td><td>29</td><td>30</td><td>31</td><td>32</td><td>33</td><td>34</td><td>35</td></tr><tr><th>Base 36</th><td>I</td><td>J</td><td>K</td><td>L</td><td>M</td><td>N</td><td>O</td><td>P</td><td>Q</td><td>R</td><td>S</td><td>T</td><td>U</td><td>V</td><td>W</td><td>X</td><td>Y</td><td>Z</td></tr></tbody></table><p>Even better, PHP takes only one line of code to convert between decimal and base36, as shown on the same Wiki page:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$base_36</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ZAQFG&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Sample Base 36 Number</span>
<span style="color: #000088;">$decimal</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;7654321&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Sample Decimal Number</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">base_convert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$base_36</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">36</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs $base_36 converted to decimal</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">base_convert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$decimal</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">36</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs $decimal converted to base 36</span></pre></td></tr></table></div><p>In our example, <em>123456</em> will be converted into its tidy equivalent base36 <em>2n9c</em> (not <em>am4k</em>, my very bad).</p><p>As for the mod_rewrite thing, as I am not a .htaccess master, I copied this thing that has been working perfectly so far from <a
href="http://www.codeigniter.com">CodeIgniter</a> forum:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;">RewriteEngine On
RewriteBase <span style="color: #339933;">/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Checks to see if the user is attempting to access a valid file,
</span><span style="color: #666666; font-style: italic;">#such as an image or css document, if this isn't true it sends the
</span><span style="color: #666666; font-style: italic;">#request to redir.php.
</span>RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_FILENAME<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!-</span>f
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_FILENAME<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!-</span>d
RewriteRule ^<span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>$ redir<span style="color: #339933;">.</span>php?code<span style="color: #339933;">=</span>$<span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#91;</span>L<span style="color: #009900;">&#93;</span>
&nbsp;</pre></td></tr></table></div><p>With this, all of the hardest parts are done, leave the remains being simple MySQL database, queries and HTML form handling. To save you from getting boring, I&#8217;ll omit it.</p><p><a
href="http://u.phoenixheart.net/" class="demo">Instead, let&#8217;s see a demo here</a><a
href="http://u.phoenixheart.net/source.zip" class="download" rel="nofollow">The source code of the demo are can be downloaded from the same server</a><br
/> <br
class="clear" /></p> <img
style='display:none' id="post-305-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/',title:'Write your own URL shortener',tweet:'With Twitter and its 140 characters limit leading all the trends, URL shortening services are in ext',description:'With Twitter and its 140 characters limit leading all the trends, URL shortening services are in ext'})"><script type='text/javascript'>document.getElementById("post-305-blankimage").onload();</script>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 3/11 queries in 0.041 seconds using disk

Served from: www.phoenixheart.net @ 2010-09-08 09:18:34 -->