Each and every time a person registers on my small site I'd like them to obtain their own subdirectory using their registered "username". Every user subdirectory will have a similar "index.php" file which is going to do something.

For instance: "/customers/username1/" and "/customers/username2/"

If someone really wants to access the subdirectory they'd simple visit: "www.example.com/customers/username1/" or "www.example.com/customers/username2/"

The simple and untidy solution is always to simply produce a subdirectory for each user and put exactly the same "index.php" file in each and every directory. But in my experience this will simply crowd my server space making my sites large.

I needed to understand if all you can do this using .htaccess? Can One create one "index.php" and something ".htaccess" file and put both of them during my "/customers/" directory? What will be the actual code which i would need to devote my .htaccess file??

For those who have an easy method to do this please tell me. I'm using Apache and PHP as my working atmosphere.

Thanks

Well, for instance, you could do this everything with one htaccess such as 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 really does:

  • switches on rewrite engine
  • inspections if your asked for file is available
  • inspections if your asked for directory is available
  • otherwise, it redirects request for your primary index.php

Essentially which means should you enter url for example yourdomain.com/customers/ivan/, you request is going to be rerouted to:

index.php?url=/users/ivan

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

That's just an example, there other mod_rewrite techniques to get this done.

Allow it to be virtual. You will find no subdirectories, you should use mod_rewrite to simulate that.

With mod_rewrite you may make /customers/username1 result in /customers.php?user=username1 for example. Things are transparent for that client, he wont notice what's really happening.

By utilizing something similar to this:

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

You are able to basically key in any directory you would like, and it'll be rerouted for your index.php page.

If you wish to make certain the present sites aren't rerouted, do that:

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

If you wish to limit the scope, so merely a subdirectory of user/ is rerouted (much like Stack Overflow), simply include 'user' to the beginning 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 lastly, if you wish to come with an individual file handle all user demands seperate out of 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 can be a much the same setup I personally use to distribute CSS files.

Note: Your Directory is going to be contained is $_GET['a']