创建使用htaccess以及PHP动态子目录

问题描述:

用户注册我的网站上每一个时间,我想他们有他们自己的子目录与他们注册的用户名。每个用户子目录将具有相同的index.php文件,该文件会做一些事情。

Every single time a user registers on my site I would like them to have their own subdirectory with their registered "username". Every user subdirectory will have the same "index.php" file which will do something.

例如:/用户/用户名1 /和/用户/ USERNAME2 /

For example: "/users/username1/" and "/users/username2/"

如果有人要访问的子目录,他们会简单到: www.example.com/users/username1/或www.example.com/users/username2 /

If some one wants to access the subdirectory they would simple go to: "www.example.com/users/username1/" or "www.example.com/users/username2/"

最简单的和凌乱的解决办法是简单地创建为每个用户一个子目录,将相同的的index.php文件中的所有目录。但对我来说这只是打算来围观我的服务器空间,并让我的目录大。

The easy and messy solution would be to simply create a subdirectory for every user and place the same "index.php" file in every directory. But to me this is only going to crowd my server space and make my directories large.

我想知道如果这一切可以使用的.htaccess做什么?我可以创建一个的index.php和一个的.htaccess文件,并把它们无论是在我的/用户/目录?什么是实际code,我会在我的.htaccess文件??

I wanted to know if all this can be done using .htaccess? Can I create one "index.php" and one ".htaccess" file and place them both in my "/users/" directory? What would be the actual code that I would have to place in my .htaccess file??

如果你有这样做的更好的办法这个请让我知道。我使用的Apache和PHP作为我的工作环境。

If you have a better way of doing this please let me know. I am using Apache and PHP as my working environment.

感谢您

嗯,比如说,你可以做这一切与一个htaccess的是这样的:

Well, for example, you could do it all with one htaccess like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

它是什么:

  • 在重写引擎开关
  • 检查是否有请求的文件是否存在
  • 检查是否有请求的目录是否存在
  • 如果不是,它重定向请求您的index.php

基本上,这意味着,如果你输入网址,如yourdomain.com/users/ivan/,您请求将被重定向到:

Basically that means if you enter url such as yourdomain.com/users/ivan/, you request will be redirected to:

index.php?url=/users/ivan

那么你$ _GET [网址]在你的index.php,分裂成碎片。

then you $_GET['url'] in your index.php and split it into pieces.

这只是一个示例,还有其他的mod_rewrite的方法来做到这一点。

That's just an example, there other mod_rewrite methods to do this.