foo.com/alice 与 foo.com/users/alice

发布于 2024-07-18 00:04:51 字数 723 浏览 6 评论 0原文

当然,为用户在您网站上的内容提供友好的 URL 是件好事。 但如何最好地做到这一点呢? 像 foo.com/users/alice 这样的东西有很多优点,最重要的是你不会弄乱你的根命名空间。 但我认为对用户来说简单性胜过一切。 许多大型网站似乎都同意这一点(我想到了friendfeed、delicious 和flickr),而这个问题是关于如何在服务器端实现这一点。

假设 alice 的真实 URL 是 foo.com/userpage?user=alice,如果有人尝试浏览不存在的用户页面(比如 foo.com/bob),他们应该访问 foo.com/createnew?user=bob 。

用户当然不应该看到上面丑陋的“真实”URL,而只是 foo.com/alice 或 foo.com/bob。 请注意,根命名空间是共享的。 例如,foo.com/help 不应翻译为 foo.com/userpage?user=help。

想必我要求一些简单的 mod_rewrite 规则,但也许有一些我没有想到的完全不同的方法。 无论如何,我认为最好记录这个常见问题的明确或“最佳实践”解决方案。

PS:请随意评论 alice.foo.com 或 users.foo.com/alice 等其他替代方案的优点。

PPS:我想我已经在其他问题中看到过这个问题的争论,但似乎很难搜索。 欢迎指点! 当然,还有其他关键字可以使其更易于搜索。 关键词:用户空间、全局命名空间、URL 命名空间。

It's of course nice to give users friendly URLs for their content on your site. But how best to do that? There are a lot of advantages to something like foo.com/users/alice, most importantly that you aren't cluttering up your root namespace. But I think simplicity for users trumps all that. A lot of big sites seem to agree (friendfeed, delicious, and flickr come to mind) and this question is about how to accomplish that on the server side.

Let's assume the real URL for alice is foo.com/userpage?user=alice and that if someone tries to surf to a nonexistent user page (let's say foo.com/bob) they should reach foo.com/createnew?user=bob.

The user of course should never see the ugly "real" URLs above, just foo.com/alice or foo.com/bob. And note that the root namespace is shared. For example, foo.com/help should not get translated to foo.com/userpage?user=help.

Presumably I'm asking for some simple mod_rewrite rules, but perhaps there's some completely different approach to this that I'm not thinking of. In any case, I thought it would be good to record a definitive or "best practice" solution to this common question.

PS: Feel free to comment on the merits of other alternatives like alice.foo.com or users.foo.com/alice.

PPS: I think I've seen this issue debated in other questions but it seems to be tricky to search for. Pointers welcome! As well as additional keywords to make this more searchable, of course. Keywords: userspace, global namespace, URL namespace.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城旧梦 2024-07-25 00:04:51

我想说这取决于您的网站如何以用户为中心。

像 myspace 这样的网站 http://www.myspace.com/jim/ 因为该网站完全围绕用户周围。

然而,您可以注册但不重要或强制的博客或新闻网站可以从

http://www.news.com.au/users/jim/

您认为如果您正在做一个与用户相关的网站,您是否可以从 MVC 设计模式或至少是流行的 MVC 框架中受益它使用路由器来引导 URI?

如果该 URI 通过路由器发送,然后发送到 UsersController,您可以决定显示用户的个人资料,或指示他们创建该用户。 你不需要乱用mod_rewrite,除非制定一个规则,将所有对不存在文件的请求定向到index.php(或任何服务器端语言的默认值)

如果你确实想使用mod_rewrite,请尝试这些

RewriteEngine On
RewriteCond %{REQUEST_URI} !(home|contact|about) [NC] // this line may be incorrect
RewriteRule ^/users/([^/]+)/?$ userpage?user=$1 [NC,L]

规则请注意 Gumbo 建议的前导 Carat,因此它仅与 TLD 的 /users/ 匹配。

这将匹配诸如 foo.com/users/bob 之类的任何内容,并带有可选的尾部斜杠。 它不区分大小写,并将是最后应用的规则。

如果请求进来并且 $_GET['user'] 在您的数据库中不存在,您可以尝试这样的操作

$user = $_GET['user'];

if (!user_exists($user)) {

    header('Location: createnew?user=' . urlencode($user));
    exit();

}

然后在 createnew 页面上,只需执行类似这样的操作

<input type="text" name="username" value="<?php echo htmlspecialchars(urldecode($_GET['user'])); ?>" />

这将使用他们尝试的用户名自动填充用户名访问个人资料。

如果您想了解有关 PHP 和 MVC 的更多信息,请尝试使用 Google 搜索或在 Stack Overflow 上提问。

I would say it depends on how user centred your site is.

Sites like myspace are http://www.myspace.com/jim/ because the site entirely revolves around the user.

A blog or news site, however, where you can register but it isn't important or mandatory could benefit from

http://www.news.com.au/users/jim/

Do you think if you're doing a website with users you could benefit from the MVC design pattern, or at least a popular MVC framework which uses a router to direct URIs?

If that URI came through a Router, and then was sent to the UsersController, you could decide to either show the user's profile, or direct them to create that user. You would not need to mess around with mod_rewrite except to make one rule that directs all requests to non existent files to index.php (or whatever the default of your server side language is)

If you do want to use mod_rewrite, try these rules

RewriteEngine On
RewriteCond %{REQUEST_URI} !(home|contact|about) [NC] // this line may be incorrect
RewriteRule ^/users/([^/]+)/?$ userpage?user=$1 [NC,L]

Please note the leading Carat as suggested by Gumbo, so it only matches /users/ of the TLD only.

That will match anything like foo.com/users/bob with an optional trailing slash. It is case insensitive and will be the last rule applied.

If the request comes in and the $_GET['user'] does not exist in your DB, you could try something like this

$user = $_GET['user'];

if (!user_exists($user)) {

    header('Location: createnew?user=' . urlencode($user));
    exit();

}

Then on the createnew page, simply do something like this

<input type="text" name="username" value="<?php echo htmlspecialchars(urldecode($_GET['user'])); ?>" />

That will fill in the username automatically with the username they tried to access a profile with.

If you'd like to know more about PHP and MVC, try a Google search or ask a question here on Stack Overflow.

若沐 2024-07-25 00:04:51

以下规则将 foo.com/bar 形式的 URL 重写为 foo.com/userpage?user=bar ,条件是 bar 还不是服务器上的文件或目录。 将以下内容放入 .htaccess 中:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ userpage?user=$1 [NC,L]
</IfModule>

正如 Alex 的回答一样,如果用户不存在,则 userpage 脚本应重定向到 createnew :(

$user = $_GET['user'];
if (!user_exists($user)) {
  header('Location: createnew?user=' . urlencode($user));
}

正如 Knuth 所说,请注意上述代码中的错误 - 我只是证明了它是正确的,没有尝试过当我真正确认它有效时,我会更新这个答案。) PS:已确认!

The following rules rewrite a URL of the form foo.com/bar to foo.com/userpage?user=bar conditional on bar not already being a file or directory on the server. Put the following in .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ userpage?user=$1 [NC,L]
</IfModule>

As in Alex's answer, the userpage script should redirect to createnew if the user doesn't exist:

$user = $_GET['user'];
if (!user_exists($user)) {
  header('Location: createnew?user=' . urlencode($user));
}

(As Knuth says, beware of bugs in the above code -- I have only proved it correct, not tried it. I'll update this answer when I've actually confirmed it works.) PS: CONFIRMED!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文