views:

848

answers:

3

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.

For example: "/users/username1/" and "/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/"

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.

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??

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

Thank you

A: 

Make it virtual. There are no subdirectories, you can use mod_rewrite to simulate that.

With mod_rewrite you can make /users/username1 lead to /users.php?user=username1 for instance. Everything is transparent for the client, he wont notice what is really happening.

Havenard
+2  A: 

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>

What it does:

  • switches on rewrite engine
  • checks if a requested file exists
  • checks if a requested directory exists
  • if NOT, it redirects request to your main index.php

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

index.php?url=/users/ivan

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

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

Sergei
A: 

By using something like this:

RewriteEngine On
RewriteRule ^([\-_0-9A-Za-z]+)$  index.php?a=$1 [L]
You can customize RewriteRule as much as you want.

You can essentially type in any directory you want, and it will be redirected to your index.php page.

If you want to make sure the existing directories are not redirected, do this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\-_0-9A-Za-z]+)$  index.php?a=$1 [L]

If you want to limit the scope, so only a subdirectory of user/ is redirected (similar to Stack Overflow), simply add in 'user' to the start of the rule:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([\-_0-9A-Za-z]+)$  index.php?a=$1 [L]

And finally, if you want to have an individual file handle all user requests seperate from your actual index.php page:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([\-_0-9A-Za-z]+)$  users.php?a=$1 [L]

This is a very similar setup I use to distribute CSS files.

Note: The Directory will be contained is $_GET['a']

Chacha102