0

I've my domain structure as domain.com/username (just like in twitter/facebook etc).

My knowledge is that, I've to create a directory with the username and put files inside it to be view from that URL.

Is there a better way to handle the URL without creating the directories for every user and just retrieving the URL and finding out which user's page it is & loading the user data from the database from the main domain only (ie domain.com index page)??

2 Answers 2

2

mod_rewrite

http://www.workingwith.me.uk/articles/scripting/mod_rewrite

Basically, your "real" URL would look like domain.com/profile.php?name=Andrew, and it would show all the information for the user with that name. You could then use mod_rewrite and make domain.com/andrew a "shortcut" to domain.com/profile.php?name=Andrew.

Just tested this, and it worked fine:

profile.php -

<?php

echo '<h1>' . ucwords($_GET['name']) . '</h1>';

?>

.htaccess -

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ profile.php?name=$1 [L]

When I visit http://localhost/andrew, I see my name, just like I should.

1
  • Updated .htaccess so it won't attempt rewrite important stuff.
    – Andrew
    Jan 24, 2011 at 6:28
1

Yes, by rewriting all requests that aren't a real file to a PHP script that displays the requested user's data. Put this in your .htaccess file or httpd.conf to use Apache's mod_rewrite:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /username.php [L]

Then username.php can look at $_SERVER['REQUEST_URI'] to see the URL, pick out the username, and display the appropriate content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.