how to handle URL without creating directories
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 in开发者_开发技巧side 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)??
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.
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.
精彩评论