Laravel routing for short Connection generation and jump (PHP 301 redirection)

Source: Internet
Author: User

Recently, lazy cancer has been committed, and had been wanting to do things for 1 months. Short connect This before the work of sending SMS is used, the general large site will have its own short link generation site, our company also has. Of course, this time I want to get on my own server.

Short connection principle: upload the website address to MySQL to obtain the self-increment ID, and then encrypt the string output to the page. When a short connection is called, back-end decryption gets the actual URL address, and then the header function does a redirect jump (301)

  

I used the Laravel framework, before learning a little bit of a long time useless, do something small, as a review.

Routing configuration:

1 function () {2        return view (' index '); 3 }); // Show input Box page routing 4 route::get ('/geturl/{url} ', ' [email protected] '); // Ajax Insertion 5 route::get ('/{url} ', ' [email protected] '); // Jump Route

Look at the effect.

Access address is: Http://58.87.108.192/showindex

  

Front-end code: (Mostly an AJAX, request my Geturl, get a short connection)

1 $ (' a '). Click (Function (event) {2 var url = $ (' #urlInsert '). Val ();3 var reg=new RegExp ("/", "G");4 5 url=url.replace (Reg, ' &&& ');6 $.ajax ({7 type: "Get",8 URL: '/geturl/' +url,9 datatype: "HTML",Ten success:function (data) { One $ (' #urlShow '). Val (' 58.87.108.192/' +data); A         }, - error:function () { - return; the         } -     }); -});

Back-End Code:

1. Get Short connections

1 //address Inbound, get self-increment ID, return encrypted string2  Public functionGETURL ($url=false){3     if($url) {4         $url=Str_replace(' &&& ', '/',$url);5         $url=Str_replace(Array(' https://', '/http '), ',$url);6         $res= db::table (' url_tables ')->where (' url ',$url),First ();7         if(!$res){8                 return(Base_convert(Db::table (' Url_tables ')->insertgetid ([' url ' =$url]), 10,32));9}Else {Ten                 return(Base_convert($res->id,10,32)); One         } A     } -     return false; -}

2. Short link jump

1 //Implement Jump2  Public functionRediecturl ($url=false)3 {4     $id=Base_convert($url, 32,10);5     $res= db::table (' url_tables ')->where (' id ',$id),First ();6     if($res&&$url=$res-URL) {7             Header("location:http://".$url,TRUE, 301);8             Exit;9}Else{Ten             returnView (' Welcome '); One     } A}

In fact, the implementation of logic is very simple, the core code is this sentence

1 Header ("location:http://www.baidu.com",TRUE, 301); exit;

Of course there are other formulations:

1 Header (' http/1.1 301 Moved permanently '); 2 Header (' location:http://www.baidu.com '); 3 exit;

Note that the incoming URL may have a '/' special character, and I made the conversion at the front and back end.

Laravel routing for short Connection generation and jump (PHP 301 redirection)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.