URL Shortening Site

后端 未结 6 1168
-上瘾入骨i
-上瘾入骨i 2021-01-06 11:15

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 syste

6条回答
  •  梦毁少年i
    2021-01-06 11:38

    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 :-)

提交回复
热议问题