Question

Possible Duplicate:
Create vanity URLs in a LAMP configuration

Once a user registers, I want the username they have chosen to be used to generate a unique profile page viewable to anyone (not just the user). If "bob" registers, he will have a profile at website.com/bob

I have looked at similar questions that say to solve the problem with .htaccess, but they don't describe how this can be done.

Was it helpful?

Solution

You wouldn't exactly need to generate a unique profile page upon registration. You can simply make a script like profile.php which takes a parameter user to dynamically show the profile of a given user by accessing /profile.php?user=bob. Example:

<?php
$u = $_GET['user'];
echo "<h2>Profile of ".htmlentities($u)."</h2>";
?>

.htaccess comes into play when you want to rewrite the url website.com/user/bob to point to website.com/profile.php?user=bob by using the following, for example.

RewriteEngine On
RewriteBase /

RewriteRule ^/user/([0-9a-zA-Z]*)$ /profile.php?user=$1 [NC,QSA,L]

OTHER TIPS

Jimmy Sawczuck pointed an answer, but to explain it here:

What you want to achieve in you case is to access a profile page using the username in the url. Creating php file for each user is troublesome and can lead to bugs (maintanability is awfull too)

You can create a profile.php script that use an argument called username. So your link will be

mysite.com/profile.php?username=bob

However it is not the url you want. And to save you exists the Rewrite Mod of webservers (mod_rewrite for apache, search for corresponding utility for other webservers)

This mod allow you to rewrite your url to match what you want:

Eg: website.com/bob in webbrowser is rewrited website.com/profile.php?username=bob for your server.

Which mean you users can have the pretty/tiny url, but the webserver will change it to call the wanted script. So both are happy.

To do this rewriting you use a module with rewriting rules. If you use apache, you use mod_rewrite and put the rule in your website configuration file or .htaccess in the wanted directory.

For the rule check the link given by Jimmy Sawczuck

Regards

if you're using PHP, you can store the users information in a database and create a file in your directory using fopen('bob.php'). Then have bob.php call whatever functions you need to display user information.

You might need to randomize the user url as there are sure to be duplicates.

I don't believe .htaccess can accomplish this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top