0

Is anyone able to explain to me how to create dynamic page URLS with PHP that do not use variables at the end of the URL?

For example how could I website with the following URL work?

https://www.discogs.com/user/<username>/collection

Surely it's not that a folder exists for all users within the /users/ directory, so how is this done?

Another example I can think of is URL shorteners which must store data in a database. E.g. bit.ly/3vjqTCR

Apologies if this has been answered many times before, but I'm not sure what to search for to find the answer.

Josh
  • 105
  • 8
  • I use `mod_rewrite` to route all requests to a `deliver.php`. There are MANY ways to route once you're in `deliver.php`. A simple example is like: `if ($_SERVER['REQUEST_URI']=='/blog/happy-cats/')echo "content of happy cats blog post"; else if (...url...==='/user/jenny/')...`. Its common to use a framework like Laravel or Symfony or one of many others. I personally developed my own framework that lets me route in various ways. You could also look for a simple [router library](https://github.com/search?q=router+php) that JUST does routing (& not all the extra stuff frameworks do) – Reed Mar 13 '21 at 21:33
  • By "variables" I think you're referring to "query paramaters" (`?param=value&param2=value2`). For that specific case (`/user//collection/`), you might have a users database. Parse the url & determine to load the `user.collection` template for `` & load that user from the database, passing it to the template. – Reed Mar 13 '21 at 21:39
  • It sounds like you're pretty new to programming, or at least to PHP. It might be helpful to do a skillshare course or follow some Youtube tutorials or something. There's a lot of fundamentals you might gain from a few good tutorials that would take awhile to pick up if going the "search for what I need when I need it" route. But also don't get yourself stuck in tutorial land. I think once fundamentals are down fairly well, tutorials are much less helpful (but I rarely have used tutorials, so I'm not the best person to ask on that). – Reed Mar 13 '21 at 21:43

0 Answers0