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?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Being related to the subject... Url Shorteners: Destroying the Web Since 2002
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...
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
Where you can have the
id
(in base 36 to prevent exhaustion of 32 bit integers) to be the shortened id - http://host/?idand 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 ?,:*&^%$#@).
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.
I think you are quite on the right way.
One thing I would not do like you said, though, is about this part :
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 :header
function)A bit like this I guess :
(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 :
Hope this helps :-)
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...