-1

I'm currently trying to program a URL shortener, but now I have the problem that I do not know how to program the forwarding.

I've made it so far that you can shorten a URL, e.g. you want to truncate the URL of YouTube, then create an entry in a database that "links" the YouTube URL to a randomly generated ID, say this ID is illustrative 'dzUejsg' Now I want to be redirected to the linked URL when entering sampleurl.com/dzUejsg, just I do not know exactly how to do that ...

I hope you can help me. Thanks in advance for all your answers! (Sorry for bad English, I'm german.)

~ JustMarvin

Miroslav Glamuzina
  • 4,472
  • 2
  • 19
  • 33
justMarvin
  • 11
  • 2
  • 2
    Possible duplicate of [PHP header(Location: ...): Force URL change in address bar](https://stackoverflow.com/questions/7467330/php-headerlocation-force-url-change-in-address-bar) – Zak Mar 20 '19 at 22:24
  • Enter a custom field instead of an ID. As an example of a self-link. Search for the parameter from the url in the self link column. And redirect according to the result found. – Özgür Can Karagöz Mar 20 '19 at 22:25
  • but isn't it then sampleurl.com/redirect?link=jeuZwnO for example? – justMarvin Mar 20 '19 at 22:26

1 Answers1

0

If the ID stored in the database is extracted from the URL (ex. https://youtube.com/watch?q=123 and the ID is 123) then you could use the header("Location: $url") function.

For example:

$url = "https://youtube.com/watch?v=".$id; // Where id is the id from the database.
header("Location: $url");

TL;DR Use the header("Location: <location>"); function

Chris
  • 426
  • 2
  • 7
  • 18
  • No, I mean you shorten a link (which can also be a google/reddit/stackoverflow/etc. link) and this link gets an ID generated and stored together in a database And I want it, that you can enter sampleurl.com/ID to redirect to the "connected" site. Like for example bit.ly – justMarvin Mar 21 '19 at 00:04