I have a site I am working on that wants to allow users to create their own page, having a url like [url]www.site.com/name[/url]. I have the registration form built and storing their data in a database, and the “name” part of the url would be their first and last name together. How would I allow url’s like this to be valid on my site?
@NogDogSep 28.2009 — #You would probably use URL rewriting at the web server level (e.g. Apache mod_rewrite module), converting "http://example.com/foobar" to "http://exampe.com/userpage.php?user=foobar", then userpage.php would use $_GET['user'] to grab the applicable data from the database.
@NogDogSep 28.2009 — #Nope, just one that takes whatever is at the end and applies it to the URL query string value. I'm not real good with this stuff, but it might be something like: <i> </i>RewriteEngine on RewriteRule ^([^/.]+)/?$ user.php?user=$1
This would match on "http://example.com/username" or "http://example.com/username/" and change it to "http://example.com/user.php?username" (if my code is correct), but note that it would not match if you allowed a "." in user names (in which case more thinking would need to go into this).
@jrthor2authorSep 28.2009 — #so every page request would go through this rewrite rule, right? How would the rule know if what came after example.com/ is a username or not?
@NogDogSep 28.2009 — #It would not "know", it would be up to you to ensure that your web page URL naming convention will facilitate this. In the example I provided, I'm assuming there will be no "." in the the user name, and that if there would be either no "/" or only a terminal "/". Therefore a URL like "http://example.com/index.php" or "http://example.com/directory/foo/bar" would not match; only something like "http://example.com/foo" or "http://example.com/bar/" would match.
@jrthor2authorSep 28.2009 — #hmm, I will have urls like example.com/contact or example.com/admin, so I'm not sure that will work. Any other suggestions on how this could work? Maybe I should make all user pages have links like example.com/portal/username, then, the rule would look for /portal/username and redirect the user to the page. what would the rewrite rule look like if I wanted that? Would this be correct
<i> </i>RewriteEngine on RewriteRule ^portal/([^/.]+)/?$ user.php?user=$1