简体   繁体   中英

url-rewriting in the php file displaying the blog article

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM