2013-03-19 104 views
2

我有一個php文件,它將存儲在數據庫中的文章的id(來自GET變量)用於顯示它的全部內容,我的意思是,標題,作者,內容等。
當它發生的URL顯示:在php文件中顯示博客文章的url-rewriting

localhost/inicio.php?id=1 

其中id = 1是在數據庫中的物品的ID。但我想讓它顯示:

localhost/this-is-the-article 

我知道我應該編輯的.htaccess,以這種方式重寫URL:

RewriteEngine on 
RewriteRule ([a-zA-Z0-9_-]+) inicio.php?id=$1 

在這一點上,我不知道是我應該做頂部的PHP文件,顯示時服用的ID通過$_GET['id']的URL重寫,同時記住,如果ID是不同的重寫URL將隨之改變,比如文章,這樣

localhost/this-is-another-article 

回答

1

創建url場,並直接保存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 
+0

非常感謝你!這是非常有幫助的! – Rodolfo 2013-03-19 13:03:43

+0

@Rodolfo很樂意幫助你:D – 2013-03-19 13:04:25

0

通常,你需要是到URL存儲在你的數據庫訪問,並做了返回讀取基於而不是id。

這是因爲沒有辦法來映射ID,並在.htaccess文件的URL,你會寫重寫代碼 所以你的.htaccess將是這樣的:

RewriteEngine on 
RewriteRule ^(.*)$ /inicio.php?article_url=$1 

您php代碼,你只需使用這個新創建的$ _GET [「article_url」]變量從數據庫中獲取信息。