简体   繁体   English

显示博客文章的php文件中的url重写

[英]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. 我有一个php文件,该文件采用存储在数据库中的文章的id(来自GET变量),并使用它显示它的全部内容,即标题,作者,内容等。
When it occurs the url shows: 发生这种情况时,URL显示:

localhost/inicio.php?id=1  

where id=1 is the article id in the database. 其中id = 1是数据库中的商品ID。 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: 我知道我应该编辑.htaccess以这种方式重写网址:

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 在这一点上,我不明白的是我应该在顶部的php文件中做什么,该文件显示通过$_GET['id']获取$_GET['id']以重写URL时的文章,请记住,如果ID与重写的网址不同,例如,这种方式

localhost/this-is-another-article

Create url field and store directly URL like 创建url字段并直接存储网址,例如

this-is-article
this-is-another-article
.... so on

then pass url instead of id . 然后传递url而不是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. 基本上,您需要将URL存储在数据库中,然后根据该URL(而不是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: 这是因为无法在将要编写重写代码的.htaccess文件中映射ID和url,因此您的.htaccess将类似于:

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. 在您的php代码中,您只需使用此新创建的$ _GET [“ article_url”]变量从数据库中获取信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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