.htaccess中和用户帐户 [英] Htaccess and user accounts

查看:181
本文介绍了.htaccess中和用户帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道微博是如何将它的网址是这样的:twitter.com/{username}

You know how twitter writes its urls like this: twitter.com/{username}?

他们怎么做到这一点?必须有参与它的一些htaccess的编码。我尝试过,但我的code变得迷茫的时候它的用户名或其他东西,并一次又一次地显示了同样的页面。

How do they achieve that? There must be some htaccess coding involved in it. I tried it but my code gets confused when its a username or a something else and shows the same page again and again.

下面是我的code:

RewriteRule ^([^/]+)$ login/id.php?name=$1
RewriteRule ^#/([^/]+)$ tags/tag.php?name=$1

所以,如果我写www.mysite.com,它需要我的主页,如果我输入 www.mysite.com/ {随机名称不是一个注册用户} ,再次带我到了家。这在URL中的主要错误。 www.mysite.com/index www.mysite.com/index2 去,即使他们是在同一页两个不同的文件。顺便说一句,我已经设置了htaccess的隐藏文件扩展名。

So if I write www.mysite.com, it takes me to the home page, and if I type www.mysite.com/{random name that is not a register user}, it again takes me to the home. This creates a major error in the url. www.mysite.com/index and www.mysite.com/index2 go to the same page even though they are two different files. By the way, I have set in the htaccess to hide file extensions.

请帮我

推荐答案

的htaccess 不是最好的性能,并且比处理与PHP路由较为有限。首先,您可以使用的htaccess 只直接大多数请求的PHP文件:

htaccess is not best for performance and is more limited than handling the routing with php. First, you use htaccess only to direct most requests to a php file:

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

那么,你的路由可能是简单的:

Then, your routing could be as simple as:

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

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

,但还有很多工作要做在实际应用环境。它有内置的路由功能,许多PHP框架,你可能会感兴趣。任何主要的框架将包括此。

but there is a lot more to do in a real application context. There are many php frameworks that have routing features built in that you may be interested in. Any major framework will include this.

下面是一个类似previous后这个答案的性能稍微扩展版本:<一href="http://stackoverflow.com/a/20034826/1435655">http://stackoverflow.com/a/20034826/1435655

Here's a slighly expanded version of this answer on a similar previous post: http://stackoverflow.com/a/20034826/1435655

要扩大一点,大多数的路由系统使用的基本想法是,你写出来的,你接受什么会处理它们的路由。当一个请求时,这些航线进行搜索,直到找到一个匹配。想想看这样的:

To expand a bit more, the basic idea most routing systems use is you write out the routes that you accept and what will handle them. When a request comes in, those routes are searched until a match is found. Think of it like this:

$myRoutes = [
  '/' => 'home.php',
  '/about' => 'about.php'
  //etc
];

$route = $_SERVER['REQUEST_URI'];
if (in_array($route, $myRoutes)) {
  include $myRoutes[$route];
}
else {
  echo "Go to a real page. This one doesn't exist."
}

您可以展开来定义URL参数,使 /用户/ someDude 将被发送到 users.php 而像 $ routeParams ['用户名'] 将有一个价值 someDude

You could expand that to define url parameters so that /users/someDude will be sent to users.php and something like $routeParams['username'] will have a value of someDude.

这篇关于.htaccess中和用户帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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