Reputation: 105
I have a php file that takes the id (from a GET variable) of an article stored in a database and uses it to display the whole stuff of it, I mean, title, author, content, and so on.
When it occurs the url shows:
localhost/inicio.php?id=1
where id=1 is the article id in the database. But I want it to show:
localhost/this-is-the-article
I know that I should edit the .htaccess to rewrite the url this way:
RewriteEngine on
RewriteRule ([a-zA-Z0-9_-]+) inicio.php?id=$1
At this point, what I do not understand is what I should do at the top the php file that shows the article when taking the id through $_GET['id']
for the url to be rewritten, taking in mind that if the id was different the rewritten url would change as well, say, this way
localhost/this-is-another-article
Upvotes: 2
Views: 378
Reputation: 625
What you need basically is to store the URL in your database and do a return fetch based on that and not the id.
This is because there is no way to map the id and the url in the .htaccess file where you will be writing the rewrite code So your .htaccess will be something like:
RewriteEngine on
RewriteRule ^(.*)$ /inicio.php?article_url=$1
in your php code you will simply fetch the info from your database using this newly created $_GET["article_url"] variable.
Upvotes: 0
Reputation: 8179
Create url
field and store directly URL like
this-is-article
this-is-another-article
.... so on
then pass url
instead of id
.
localhost/inicio.php?url=this-is-article
localhost/inicio.php?url=this-is-another-article
then rewrite it...
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ /inicio.php?url=$1
now access it via
localhost/this-is-article
localhost/this-is-another-article
Upvotes: 1