2012-02-28 21 views
-1

我想在我的網站做個人資料頁面。現在用的哈希識別,例如:如何執行個人資料頁面:/ 1234? (沒有「#」和沒有「?id =」)

www.mysite.com/#123123 

但是我注意到,像Facebook或Twitter網站使用的網址,如:facebook.com/1234識別配置文件。

我該如何在我的網站上做到這一點?

+0

這是通過像Apache的mod_rewrite的URL重寫引擎來完成。重寫引擎匹配正則表達式,並且可以將URL的一部分(本例中的數字)作爲'$ _GET'參數傳遞給PHP文件,而不用重定向用戶的瀏覽器以反映該內容。 – 2012-02-28 03:01:14

+0

[其中很多](http://stackoverflow.com/search?q=%5B.htaccess%5D+id)將幫助您開始 – 2012-02-28 03:02:29

回答

0

隨着mod_rewrite,但根據您的控制器上,是這樣的:

RewriteEngine On 
##Do rewrite if NOT file 
RewriteCond %{REQUEST_FILENAME} !-f 
##Do rewrite if NOT dir/folder 
RewriteCond %{REQUEST_FILENAME} !-d 

##Numeric 
RewriteRule ^([0-9]+)$ index.php?id=$1 [L] 
##Or for AlphaNumeric (any case) 
RewriteRule ^([a-zA-Z0-9]+)$ index.php?id=$1 [L] 

##Or for Anything after first slash 
RewriteRule ^(.*)$ index.php?route=$1 [L] 
##Note using this method you would need a (Router) controller that splits 
## the request. 
##Eg site.com/id/Af13s passes index.php?route=id/Af13s then with 
## explode('/',$_GET['route']) you can get the action/sub action 

##or have multiple entry's for each section 
RewriteRule ^id/([a-zA-Z0-9]+)$ index.php?id=$1 [L] 
RewriteRule ^page/([a-zA-Z0-9_]+)$ index.php?page=$1 [L] 
RewriteRule ^admin/([a-zA-Z0-9_]+)$ admin.php?route=$1 [L] 

希望它可以幫助

+0

即使ID是非數字而沒有衝突時,我如何才能使其工作與子目錄? – lisovaccaro 2012-02-28 19:55:17

+0

@ Liso22答案更新後,希望它有幫助 – 2012-02-29 02:06:30

+0

是的,它確實。謝謝 – lisovaccaro 2012-02-29 04:37:32

0

很可能他們正在使用的.htaccess或類似於/ 1234重定向至profile.php?ID一些腳本= 1234

要回答你的問題比較好,考慮mod_rewrite的。此外,this可能有幫助,特別是「創建美麗的網址」部分。