2011-08-25 45 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中使用重写服务。