本文介紹了使用 apache 的 mod_rewrite 解析 SEO 友好的 URL的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我如何轉換類似的東西
me.com/profile/24443/quincy-jones
到
me.com/profile.php?id=24443
或類似的東西
me.com/store/24111/robert-adams
到
me.com/store.php?id=24111
使用 mod_rewrite?
with mod_rewrite?
我也可以使用 mod_rewrite 進行反向轉換,還是必須通過 PHP 解析?
Can I make the reverse conversion as well with mod_rewrite, or would I have to parse it through PHP?
推薦答案
這應該適用于兩者:
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+).*$ $1.php?id=$2 [L]
說明:
^ - beginning of the string
([^/]) - first group that doesn't contain /
will match both 'profile' and 'store'
will also be referenced by $1 later
/ - first slash separator
([^/]) - second group, id in your case
will be referenced by $2
.* - any ending of the request uri
$ - end of request string
您還可以使其更精確,以便僅重寫兩個請求并且僅接受數字作為 id:
You can also make it more precise so only the two request are rewritten and only digits are accepted as id:
RewriteRule ^((profile|store))/(d+).*$ $1.php?id=$2 [L]
這篇關于使用 apache 的 mod_rewrite 解析 SEO 友好的 URL的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!