2011-08-25 43 views
0

可能重複:
How to create friendly URL in php?技術來創建用戶友好的URL

我有轉向

profile.php?id=24到的東西更方便用戶閱讀了大量的內容在谷歌。

我怎麼會去重定向鏈接像

www.site.com/profile/username 

profile.php?id=userid (find from username) 

感謝

+2

可能重複的[如何在PHP中創建友好的URL?](http://stackoverflow.com/questions/812571/how-to-create-friendly-url-in-php)和[我怎樣才能使URL搜索引擎友好?](http://stackoverflow.com/questions/1653520/how-can-i-make-the-url-search-engine-friendly)和[噸的其他](http:// stackoverflow。 COM /搜索?q = + PHP +用戶友好+網址&提交=搜索)。在提出問題之前,請使用搜索。 –

+1

實際上,初始鏈接通常更像'www.site.com/profile/userid/username'。用戶名只是爲了方便,並沒有用在網址翻譯中。看看SO如何工作。 –

+0

你可以做一些研究。這個問題的答案在Google上已經發現了530萬次 – dtech

回答

1

這可以通過Apache mod_rewrite RewriteEngine來實現。一個例子的.htaccess:

RewriteEngine On 
RewriteRule ^/profile/username/([\d]+)$ profile.php?id=$1 
0

你可以做到這一點與apache rewrite rules

Apache將在內部重寫一個URL,如/profile/usernameprofile.php?username=username

例子:(這個地方在的.htaccess在同一目錄比profile.php)

如果profile.php腳本不接受username參數,你還可以包括用戶ID在用戶友好的URL:

RewriteEngine On 
RewriteRule ^/profile/(.*)-(\d+)$ profile.php?id=$2 

這將改寫像/profile/username-id網址來profile.php?id=id

見apahe mod_rewrite documentation

0

這是通過在httpd.conf中定義規則.htaccess文件創建的重寫規則,來完成。 這適用於Apache。我不知道如何在其他服務器上執行此操作。

0

當您的用戶註冊時,使用PHP或CGI腳本創建一個名爲「Username.txt」的文件並將其存儲在名爲Profiles的文件夾中(就像您使用的那樣)。在文本文件中,使其生成數字(向上計數或散列?)或在Google或Apache中使用重寫服務。