自定义网址 - 给每个用户一个网址 [英] Custom urls - giving each user a url

查看:34
本文介绍了自定义网址 - 给每个用户一个网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个网站,我想自定义用户的个人资料,以便他们的个人资料网址共享他们的姓名.例如,网站域为 www.example.com,用户的 url 为 www.example.com/username.

I am building a website and I would like to customize the users' profile so their profile url shares their name. For example, the website domain would be www.example.com and the users' url would be www.example.com/username.

我假设这是一个惯例,因为我在网络上到处都看到了这一点.这是通过为每个用户提供他们自己的目录来完成的吗?这有多辛苦?

I am assuming this is a convention because I see this all around the web. Is this done by giving each user their own directory and how painstaking would that be?

推荐答案

对于这样的事情,我真的无法证明使用 htaccess 是合理的.我只使用 htaccess 通过一个 php 文件(我的根 index.php)来路由所有内容,并让 php 整理出如何处理 url.例如,您可以这样做:

I really can't justify using htaccess for something like this. I only use htaccess to route everything through one php file (my root index.php) and let php sort out how to handle the url. For example, you could do this:

$uri = trim($_SERVER['REQUEST_URI'], '/');
$pieces = explode('/', $uri);
$username = $pieces[0];

然后用 $username 做一些事情.

Then do something with $username.

我解析和使用我的网址的方式比这更复杂一些,但它与您的问题无关.只要确保您所做的任何事情都能够解释任何可能的查询字符串等.

The way I parse and use my url's is a bit more complicated than this, but it wouldn't be relevant to your question. Just make sure whatever you do is able to account for any possible query strings, etc.

mod-rewrite 对性能来说不是很好,所以我不会滥用它.

mod-rewrite is not great for performance, so I wouldn't abuse it.

这里只是对我的原始代码示例稍作扩展:

Here's just a slight expansion on my original code sample:

//.htaccess - route all requests through index.php
RewriteCond %{REQUEST_URI} !.(png|jpe?g|gif|css|js|html)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L]

这是您可以在 index.php 中执行的操作的示例:

and this is an example of what you could do in index.php:

$pieces = preg_split('-/-', $_SERVER['REQUEST_URI'], NULL, PREG_SPLIT_NO_EMPTY);
$username = $pieces[0];

include "users/{$username}.php";

现在您可以访问 mysite.com/myUserNameHere,请求会转到 index.php,它会解析出用户名并包含该用户的文件.

Now you could visit mysite.com/myUserNameHere, the request goes to index.php which parses out the username and includes a file for that user.

这就像你问的那样处理路由.我说我的路由比较复杂,但是我的使用比较复杂,所以没有相关性.我只是警告我这里的代码没有考虑附加查询字符串的 url,比如mysite.com/someUser/?foo=bar".这是在没有查询字符串的情况下解析 url 的简单过程,您应该能够处理它.如果您在解析 url 时需要进一步帮助,请发帖询问.

That takes care of the routing just like you asked. I said that my routing is more complicated, but my use is a lot more complicated, so it isn't relevant. I only warned that my code here doesn't account for a url with a query string attached, like "mysite.com/someUser/?foo=bar". It's a simple process to parse the url without the query string and you should be able to handle that. If you need further assistance parsing the url, then make a post asking about that.

这篇关于自定义网址 - 给每个用户一个网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆