与社区URL样式(如Last.FM或Wikipedia)怎么办? [英] What to do with a community URL style like Last.FM or Wikipedia?
问题描述
我试图了解我应该如何使用URLs
中的字符,这是因为我正在构建一个站点,用户可以在其中存储内容并通过在URL
中数字化其名称来访问内容页面.
I'm trying to understand how I should work with characters in URLs
, this because I'm building a site where the user can store content and go to the content's page by digiting it's name in the URL
.
因此,类似于Wikipedia
或Last.FM
网站.
我在网站上看到,用户可以编写
http://it.wikipedia.org/wiki/Trentemøller
之类的东西,并且可以访问艺术家的页面.
I see in the site, user can write something likehttp://it.wikipedia.org/wiki/Trentemøller
and the page of the artist can reached.
页面加载后,如果我复制该URL,则写为:
http://it.wikipedia.org/wiki/Trentemøller
,但是如果将其粘贴到文本编辑器中,它将被粘贴为
http://it.wikipedia.org/wiki/Trentem%C3%B8ller
after the page is loaded, if I copy the URL i see written as:http://it.wikipedia.org/wiki/Trentemøller
but if I paste it into a text editor, it will be pasted as
http://it.wikipedia.org/wiki/Trentem%C3%B8ller
因此将字符ø
粘贴为%C3%B8
对于这样的URL(艺术家小林武史的页面)当然也是如此
of course the same is for URLs like this (the page of the artist Takeshi Kobayashi)
http://www.last.fm/music/小林武史
http://www.last.fm/music/%E5%B0%8F%E6%9E%97%E6%AD%A6%E5%8F%B2
http://www.last.fm/music/小林武史
http://www.last.fm/music/%E5%B0%8F%E6%9E%97%E6%AD%A6%E5%8F%B2
如果我在第一位或第二位输入数字,那么无论如何该页面都可以正常工作,为什么?
If I digit the first or the second, the page works in any case, why?
我想我应该对.htacces
和mod_rewrite
做些什么,但是我不确定,特殊字符是否会自动转换为url特殊字符?
I think I should do something with the .htacces
and mod_rewrite
but I'm not sure, are the special chars automatically converted to the url special chars?
然后,如何让PHP使用内容名称进行正确的查询?
and then, how can I do to let PHP do the right query with the content name?
如果我有一个像这样的表
if I have a table like
table_users
- username
- age
- height
- weight
- sex
- email
- country
我可以使用mod_rewrite
编写一个http://mysite.com/user/bob
这样的地址,以从table_users
获取username
bob,但是http://mysite.com/user/小林武史
呢?
I'm able with mod_rewrite
to write an address like http://mysite.com/user/bob
to get the username
bob from the table_users
but what about http://mysite.com/user/小林武史
?
在这里,我展示了一个我想做的简单例子:
here I show a simple example of what I think to do:
#.htaccess
RewriteEngine On
RewriteRule ^(user/)([a-zA-Z0-9_+-]+)([/]?)$ user.php?username=$2
<?php
// this is the page user.php
// this is the way I use to get the url value
print $_REQUEST["username"];
?>
这行得通,但仅限于[a-zA-Z0-9 _ +-],如何在不损失过多安全性的情况下与其他字符更兼容?
this works, but it's limited to [a-zA-Z0-9_+-], how to be more compatible with all chars like the others without loss too much security?
有人知道避免麻烦的方法吗?
Did someone know some way to avoid troubles?
推荐答案
大多数浏览器urlencode()
小林武史
到%E5%B0%8F%E6%9E%97%E6%AD%A6%E5%8F%B2
.
维护您的.htaccess
mod_rewrite
规则,您是否考虑过使用类似的东西:
Reguarding your .htaccess
mod_rewrite
rules, have you considered using something like:
RewriteEngine On
RewriteRule ^(user/)(.+?)[/]?$ user.php?username=$2
这篇关于与社区URL样式(如Last.FM或Wikipedia)怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!