2013-04-30 63 views
1

我想創建一個動態url,這樣當我從我的網站登錄時,我的用戶名將顯示在url上。例如,假設我登錄有:如何使用您的用戶名創建動態網址?

http://example-website.com

與我的用戶名MYNAME。 URL應該成爲

http://example-website.com/myname

但實際上網頁是loginsuccess.php與MYNAME的別名,這是我的用戶名

我怎樣才能做到這一點?

+0

[Facebook的,如自定義配置文件的URL PHP(可能的重複HTTP ://堆棧overflow.com/q/10595556/254830)作爲RakeshS提到。 – doppelgreener 2013-04-30 04:41:58

回答

5

這實際上很容易與.htaccessRewriteEngine。在下面的例子中,我將使用一些非常簡單的(.*)正則表達式,它只是一個通配符,因此一切都會被接受(a-zA-z0-9)。

/username前綴profile

RewriteEngine on 
RewriteRule ^profile/(.*) user_profile.php?username=$1 
ErrorDocument 404 /pathtofile/404.html 

結果:http://www.example-website.com/profile/john-smith

只有username工作,此選項將需要某種Routing類的。

RewriteEngine on 
RewriteRule ^(.*) user_profile.php?username=$1 
ErrorDocument 404 /pathtofile/404.html 

結果:http://www.example-website.com/john-smith

使用RewriteEngine敘述與後綴auth

RewriteEngine on 
RewriteRule ^(.*)/auth auth_user.php?username=$1&q=auth 
ErrorDocument 404 /pathtofile/404.html 

結果:http://www.example-website.com/john-smith/auth

1

快速提示:在一個表中

  • 商店唯一的用戶名。
  • 在你的http服務器(here's a tutorial on that)中啓用重寫模塊。
  • 將URL http://example.com/username重寫爲route.php(或類似內容)。
  • Route.php應該讀取URL並提取用戶名並用適當的表格進行交叉驗證。
  • 如果找到匹配,則顯示相應的頁面,否則顯示404。
  • 完成!

有關詳細說明,請參閱Facebook Like Custom Profile URL PHP

相關問題