2012-02-28 44 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可能有帮助,特别是“创建美丽的网址”部分。

相关问题