skip to Main Content

I am Creating a URL shortning service with core php, I created a Database storing long and short (generated after submission of the form) URL’s. Is there any way i can make it like this that i dont have to use a page to acess the short URL, Let me explain in detail.

  • I have a index.php and it has a form, it takes a URL or long URL from the user and after validation of the URL i save the Long Url with a short 5-6 character string, i save this to my database, and i will send back the short URL to the User.

  • To acess the short URL i created another page called link.php and i am listining for the querystring in this page request like this http://localhost:80/link.php?q=d3432hnc.

Ans i am looking for this question –

So i am asking that is there any other way i can acess the short url just like https://localhost:80/jfkdasjfkajsd without making a page and passing this url in the queryStrings like http://localhost:80/link.php?q=jfkdasjfkajsd.

2

Answers


  1. Search for “php URL rewriting”.

    You can setup Apache or nginx to rewrite the first url into the second.

    Login or Signup to reply.
  2. look into mod_rewrite module and .htaccess of apache/php

    In essence, it allows treating URL segments as parameters and passing them to a script

    .htaccess

    Options FollowSymLinks
    ReWriteEngine on
    RewriteCond %{REQUEST_FILENAME}  !-f
    RewriteCond %{REQUEST_FILENAME}  !-d
    RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]
    

    now if your website is called by an URL http://website.com/this/is/great

    index.php gets invoked and URL segments are passed via GET parameter

    In index.php you can use:

    $url = explode('/', htmlspecialchars($_GET['q']));
    

    to get an array of URL segments. Use them to figure out what file to return

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search