Short web site has been popular for some time, used to do Sina Weibo application when there is contact, but did not make clear, and recently again contacted this dongdong, carefully studied the next, found that the short URL is also quite easy. The following will be the generation of short web site ideas and the use of PHP to generate a short web site to do a record of the implementation method.
the idea of generating a short URL: If the short URL restored, you know what it looks like? Like what:
Http://www.Alixixi.com/javascript-function/834.html
For the above link, there is a way to open it, in addition to the direct opening, as follows:
Http://www.Alixixi.com/link.php?url=http://www.Alixixi.com/javascript-function/834.html
OK, the short URL restore is actually like this, you may see Sina Weibo application in the short URLs are like this:
Http://t.cn/zHEYrvV
In fact, he restored it might be like this:
Http://t.cn/link.php?url=http://www.Alixixi.com/php-template-framework/832.html
Well, here's the second step, how to
Http://t.cn/link.php?url=http://www.Alixixi.com/php-template-framework/832.html
Shrink into
Http://t.cn/zHEYrvV
This place requires URL rewriting, which can be overridden in this example:
Rewriteengine on
rewritebase/
rewriterule ^/(. *) $ link.php?url=$1[l]
Here is achieved will http://t.cn/link.php?url=zHEYrvV conversion to Http://t.cn/zHEYrvV, shortened a lot, then how to find http://www through the zheyrvv. Alixixi.com/php-template-framework/832.html This web site and jump to this URL to go up? A cryptographic algorithm is used to shorten all long URLs to a corresponding 5-6-bit and unique string, and to deposit the correspondence into the database. The combination of this example is based on the incoming parameters zheyrvv to the database to find the corresponding URL, found the header jump past.
OK, as for the idea of creating a short web site is like this.
let's share the process of creating a short URL via php (this will generate a long URL for a length of up to 5-6 characters and is also required to be unique):
<?php
function code62 ($x) {
$show = ';
while ($x >0) {
$s = $x%;
if ($s >35) {
$s =chr ($s +61);
} ElseIf ($s >9&& $s <=35) {
$s =chr ($s +55);
}
$show. = $s;
$x =floor ($x/62);
return $show;
}
function Shorturl ($url) {
$url =crc32 ($url);
$result =sprintf ("%u", $url);
Return code62 ($result);
Like what
echo shorturl (' http://www.Alixixi.com/');
will generate a unique corresponding code for n2q8e, OK, as to how to do URL rewriting and database storage here is not much written, according to their own situation to come.