Short Web Site Inquiry, Web site inquiry. Short, dwz. cnCSWuPhttp: short URL inquiry
Introduction
Short URLs are common, such as the following:
Http://dwz.cn/CSW6Y> http://www.cnblogs.com/iforever/p/4313704.html,
Http://dwz.cn/CSWuP => http://www.cnblogs.com/iforever/p/4279006.html, etc.
When you access these websites, you can direct them to the same page one by one. here I just give an example. these short websites are often seen in weibo or similar social networks, because the original link address is too long, the short URL will be very powerful and easy to remember, but it also has its shortcomings, for example, the short website may not directly see some information in the website (some information can be seen in the long website ).
I have been in touch with this all day, so that I have branded "nothing" in my thoughts before I really understood his principles. This misses some knowledge, this is a psychological suggestion. Does anyone know it?
Principle
First, I guess this is implemented through redirection. dwz.cn is a server that receives some short URLs. these short URLs are processed by dwz.cn, in dwz.cn, there is a record of the long URL corresponding to the short URL, then obtain the long URL corresponding to the short URL from the database, and then 302 redirection. See if this is the case.
Access http://dwz.cn/csw6ythrough a browser to analyze the request (chrome debugging tool or wireshark can be used here) and obtain the following data:
Name: abitversion: 1 handle:-rewrite: if (! -D &&! -F & path ~ "/(. *) $") Goto "do. php? Url = $1 & act = out"
Handle: the rewrite rule is followed. the detailed rules of Sina Cloud are shown here.
After the redirection is complete, you must note that urlencode is required when passing parameters. urldecode is used before redirection. When urlencode is not used, url-type parameters are passed, and some information may be lost during retrieval. Therefore, the url must be escape processed when parameters are passed before the short link is generated, encode a special string. when accessing a short link, you need to urldecode the encoded url to restore it to a normal link, otherwise, the link will not be treated as a normal url when the header is redirected. after the url is redirected, it will be appended to the host on the previous page, which is similar to http://abit.sinaapp.com/www.cnblogs.com. this may cause an error.
When the sae is redirected, there will be a problem. it will automatically filter the double backslash into one, for example, from the forward.
Encoding
Main processing part
> 6;} return $ out;} public function retJson ($ arr) {return json_encode ($ arr);} // map the url and save public function dispose ($ url, $ act) {$ mysql = new SaeMysql (); switch ($ act) {case 'in': $ short = $ this-> long2short ($ url ); $ url = addslashes ($ url); $ SQL = "insert into 'Tiny _ url' ('Short ', 'long') values (' {$ short }', '{$ url}') "; $ mysql-> runSql ($ SQL); if ($ mysql-> errno ()! = 0) {echo "generation failed";} else {echo" http://abit.sinaapp.com/ {$ Short} ";}break; case 'out': if (strlen ($ url)> 5) echo $ this-> retJson (array ("code" => "-1", "msg" => "no such record ")); $ SQL = "select * from 'Tiny _ url' where 'short '=' {$ url} 'limit 1"; $ data = $ mysql-> getData ($ SQL ); if (! $ Data) {echo $ this-> retJson (array ("code" => "-1", "msg" => "no such record "));} else {$ location = urldecode ($ data [0] ['long']); header ("Location: {$ location}"); exit () ;}break; default: # code... break ;}}$ url = isset ($ _ GET ['URL'])? $ _ GET ['URL']: null; $ act = isset ($ _ GET ['AC'])? $ _ GET ['AC']: null; $ snapshotUrl = new snapshotUrl (); if ($ url = null | $ act = null) echo $ snapshotUrl-> retJson (array ("code" => "-1", "msg" => "Parameter error"); $ snapshotUrl-> dispose ($ url, $ act );
Result
I made a small webpage that can be tested:
Http://abit.sinaapp.com/you can try it if you are interested.
The copyright of this article is owned by the author iforever (luluyrt@163.com), without the author's consent to prohibit any form of Reprint, repost the article must be in the obvious position on the article page to give the author and the original connection, otherwise, you are entitled to pursue legal liability.
Introduction of short Web site is commonplace, such as the following http://dwz.cn/CSW6Y=http://www.cnblogs.com/iforever/p/4313704.html, http://dwz.cn/CSWuP=http...