URL Shortening Site

2019-02-14 14:50发布

I am working on a URL shortening site which uses PHP, MySQL and Apache. General idea of URL shortening as I look at open source projects: user gives a URL link and the system gets ID for that link from database. Then convert the ID X base system (I am using base 36). Then use Apache mod_rewrite and create shortened URL and then redirect. Do all of the URL shortening sites work like that or do some use a different algorithm other than this? And any idea for making a difference from other URL shortening systems?

6条回答
甜甜的少女心
2楼-- · 2019-02-14 15:13
我只想做你的唯一
3楼-- · 2019-02-14 15:17

Just a security note: Do not redirect directly to the site from a shortened url if it's not under your control/domain - have a landing page where the user can see the actual url and decide whether to continue or not...

查看更多
We Are One
4楼-- · 2019-02-14 15:23

You can use bit.ly (twitter uses this). There are some APIs which you can use to call and fetch shortened URLs.

Also talk about shortening URLs, you can simply use a table like this

CREATE TABLE `urls` (
  `id` varchar(255) NOT NULL default '',
  `url` text NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Where you can have the id (in base 36 to prevent exhaustion of 32 bit integers) to be the shortened id - http://host/?id

and when you call the URL http://host/?As2dD24B, it will look up the matching ID and URL, then redirects to the URL. simple?

Also keep in mind that you can expand your base 36. I am assuming that your base 36 is: a-z and 0-9. You can add in A-Z (another 26) and other symbols (such as ?,:*&^%$#@).

查看更多
Fickle 薄情
5楼-- · 2019-02-14 15:35

I would suggest using YOURLS which is a robust open source package to do exactly this. It's PHP/MySQL based.

http://yourls.org/#Install

From the about page:

YOURLS is a small set of PHP scripts that will allow you to run your own URL shortening service (a la TinyURL). You can make it private or public, you can pick custom keyword URLs, it comes with its own API.

查看更多
再贱就再见
6楼-- · 2019-02-14 15:37

I think you are quite on the right way.

One thing I would not do like you said, though, is about this part :

then use apache mod_rewrite and create shorten url and then redirect.

I don't think I'd create an Apache RewriteRule, nor use mod_rewrite.


When receiving an short url, like short.com/MYID, Id would :

  • decrypt the "MYID" part to the id number in DB
  • fetch the URL from database
  • just redirect to that URL from some server code (like PHP, using the header function)

A bit like this I guess :

// fetch $urlFull from DB (corresponding to the MYID received in GET)
header('HTTP/1.x 301 Moved Permanently');
header('Location: ' . $urlFull);
die;


(edit) If by mod_rewrite you meant "transform short.com/MYID to short.com/id=MYID", oh, yes, in this case, of course !

I'm using something like this on one of my sites, btw :

RewriteEngine on
RewriteCond %{REQUEST_URI}  !^/index.php
RewriteRule ^(.*)$ /index.php?hash=$1   [L]


Hope this helps :-)

查看更多
我命由我不由天
7楼-- · 2019-02-14 15:37

If you want to do something different from other URL shortening sites, figure out a way to make sure the links don't break if your site goes away! I don't know how to do this, I think it's probably impossible...

查看更多
登录 后发表回答