Aim:
I want to generate seo-friendly URLs for pages that have an id url when created. I'm using Apache, MySQL and PHP.
I have an id field in the database and a url field as well for each new /newpage.php I create. When I create new pages using home/newpage.php their url becomes home/newpage.php?id=1. I would like to make that into home/my-custom-url-example using the url field for that page and get it from the database just like id is being fetched.
Question:
Can I somehow make this happen with Rewrite Rules in the .htaccess file?
What I have until now:
In my .htaccess file for now I have turned on the Rewrite mode. I have then successfully removed any .php from any urls that end with that.
RewriteEngine On
# remove .php extension from url
RewriteRule ^([^\.]+)$ $1.php [L]
I also managed to write a one-off working seo-friendly url for one of my pages but that wont work long term. Also, the below code just makes and extra url serve the same content and does not get rid of my old url.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^my-custom-url-example /newpage.php?id=1 [QSA,L]
So with the code above I get 2 urls serving the same page:
home/my-custom-url-example and home/newpage.php?id=1 which is not what I want.
I'm quite new to this so any help will be greatly appreciated!