![](/img/trans.png)
[英]URL-Rewriting: Rules work for some .php pages, but not for others.
[英]url-rewriting in the php file displaying the blog article
我有一个php文件,该文件采用存储在数据库中的文章的id(来自GET变量),并使用它显示它的全部内容,即标题,作者,内容等。
发生这种情况时,URL显示:
localhost/inicio.php?id=1
其中id = 1是数据库中的商品ID。 但我希望它显示:
localhost/this-is-the-article
我知道我应该编辑.htaccess以这种方式重写网址:
RewriteEngine on
RewriteRule ([a-zA-Z0-9_-]+) inicio.php?id=$1
在这一点上,我不明白的是我应该在顶部的php文件中做什么,该文件显示通过$_GET['id']
获取$_GET['id']
以重写URL时的文章,请记住,如果ID与重写的网址不同,例如,这种方式
localhost/this-is-another-article
创建url
字段并直接存储网址,例如
this-is-article
this-is-another-article
.... so on
然后传递url
而不是id
。
localhost/inicio.php?url=this-is-article
localhost/inicio.php?url=this-is-another-article
然后重写它...
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ /inicio.php?url=$1
现在通过访问
localhost/this-is-article
localhost/this-is-another-article
基本上,您需要将URL存储在数据库中,然后根据该URL(而不是ID)进行回访。
这是因为无法在将要编写重写代码的.htaccess文件中映射ID和url,因此您的.htaccess将类似于:
RewriteEngine on
RewriteRule ^(.*)$ /inicio.php?article_url=$1
在您的php代码中,您只需使用此新创建的$ _GET [“ article_url”]变量从数据库中获取信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.