[英]URL rewrite with PHP & MySQL and htaccess
我在此頁面上將我的站點localhost/tutorials/rewrite/news.php
為localhost/tutorials/rewrite/news.php
,這些都是來自數據庫中新聞文章的標題。 每個標題都是閱讀文章的鏈接。 鏈接是
'<a href="read-news.php?url=' . $url . '">' . $title1. '</a>'
read-news.php頁面獲取URL並在sql語句中使用它以從數據庫獲取文章
$url = $_GET['url'];
$sql = "SELECT * FROM news WHERE url = '$url'";
我在news.php頁面上的鏈接和read-news.php上的URL看起來像這樣
localhost/tutorials/rewrite/read-news.php?url=the-news-today
我怎樣才能看起來像
localhost/tutorials/rewrite/read-news/the-news-today.php
我使用了以下htaccess代碼,通過查看其他示例,我認為該代碼足以解決它
RewriteRule ^read-news/(.*).php /read-news.php?url=$1 [PT]
任何幫助請
做到Wordpress如何做到這一點。 將所有內容重寫為index.php並從腳本解析URL。
編輯:您的RewriteRule也需要一些工作。 嘗試這個:
RewriteRule ^read-news/(.*)\\.php$ /read-news.php?url=$1 [PT]
(您需要使用“ $”來表示重寫已結束,並在“ php”前使用反斜杠轉義。)
另外,您應該將頁面鏈接到您希望它們如何顯示:
href="localhost/tutorials/rewrite/read-news/the-news-today.php"
.htaccess不會神奇地為您更改網址。
另外,如果您希望能夠訪問查詢字符串變量,我會在您的標志中添加QSA: [PT,QSA]
這樣,您仍然可以使用$_GET['url']
訪問url變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.