dynamic file names for php like in js can it be done?

you know how in js you can have file names like [id].js where id is a field from a database for pages that are changing based on what is pulled from the db? Can you do the same for php like [user_id].php for a building something like a individual user page?
10 Replies
Jochem
Jochem10mo ago
that's not really a dynamic filename, but a way to represent a rewritten URL Are you using Apache or nginx?
MD
MDOP10mo ago
probably going to be apache
MD
MDOP10mo ago
TIL
Jochem
Jochem10mo ago
Because of the way PHP files are served, which is fundamentally different from how javascript backends work which have a continuously running node server, you do the rewriting in your server software for PHP Even frameworks like laravel that abstract that away from you with routing, do it by redirecting all traffic to a routing PHP file which then distributes your traffic accordingly by including and running other php files based on the URL, but at the base it's all a mod_rewrite rule that points to the router php the way I use it normally, for vanilla apps that is, is to just write the mod_rewrite rules myself the most common one is usually to redirect say "/login" to login.php and "/about" to about.php, but also to redirect "/article/4/" to article.php?articleid=4
MD
MDOP10mo ago
in my case they would successfully login then redirect to their page which would be in the url their [user_id]
Jochem
Jochem10mo ago
you'd redirect to /user/5 and have mod_rewrite redirect to user.php?userid=5
MD
MDOP10mo ago
ill need to add these rules to apache because login.php would look better as /login do you call mod_rewrite in your php code?
Jochem
Jochem10mo ago
you put it in .htaccess in the root folder or any subfolders, then apache reads it as part of serving a request it doesn't necessarily have anything to do with PHP, it's pure Apache stuff an alternative is to write a router yourself, or use a framework, but mod_rewrite is fine for 99% of cases
MD
MDOP10mo ago
ill give it a try ill probably update this post with issues
Want results from more Discord servers?
Add your server