How to assign a webaddress to each item fetch from the database, like www.example.com/datafromdatabase

29 Views Asked by At

I want to fetch data from the database and assign it to a web address base on it id. For example if I fetch 5 data from database I want to assign those data to a web address without using Get method.

Just like this www.example.com/datafromdatabase instead of this www.example.com?id=datafromdatabase. I have seen many forum site using the "/" but I only know the get "?" method.

1

There are 1 best solutions below

1
sjdaws On

You're looking for rewrites in this situation, for Apache you will want to use rewrite rules:

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

For nginx you also want to use try_files:

try_files $uri $uri/ /index.php?id=$uri;

Both of these will convert the /directory to index.php?id=directory if the file doesn't exist which will allow images and such to still be served as normal and only rewrite things which aren't files or directories.