[英]How to create a beautiful post links without ID
我有這樣的鏈接:
http://localhost/mywebsite/post.php?title=my-post-title&id=2
要么
http://localhost/mywebsite/post/my-post-title/2/
(使用mvc)
但我需要顯示沒有ID的鏈接,就像互聯網上的許多網站一樣。
例如:
localhost/mywebsite/post/my-post-title/
您需要使用mod_rewrite
來美化您的URL。 查看此鏈接: http : //www.sitepoint.com/guide-url-rewriting/
以下是上面的mod_rewrite
示例:
RewriteEngine on
RewriteRule ^mywebsite/(.*)/(.*)$ mywebsite/post/?title=$2 [QSA]
Via - 如何美化URL?
URL必須包含唯一標識符。 沒有它,您的應用程序無法知道要顯示哪個帖子。 在這種情況下,您需要為帖子創建第二個唯一標識符(除了整數ID):這稱為slug 。
因此,除了id
和title
列之外,添加一個帶有UNIQUE
鍵的slug
列。 此列應該是帖子標題的URL安全字符串轉換(例如,“這是我的第一篇帖子!”的標題可能會給出一個“這是我的第一篇帖子”的標題)。 如果slug已經在使用中,這可能需要預先添加一個額外的字符(或更多),即如果你有兩個標題為“This is a post”的帖子,那么第一個將有一個slug“this-is-a-post”而第二個將有“這是一個后2”的slu ..
一旦創建了slug並將其分配給post,這將被用作URL中的標識符。 使用mod_rewrite
你將http://localhost/mywebsite/post/my-post-title
重寫為http://localhost/mywebsite/post.php?slug=my-post-title
,然后查詢數據庫中的帖子slug = $_GET['slug']
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.