簡體   English   中英

顯示博客文章的php文件中的url重寫

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM