I am trying to create a page with profiles similar to facebook. The question I have is: instead of having a site that says www.sitename.com/profile.php?username=Username as the URL, I want the page to say www.sitename.com/Username.
I know to show a URL like the latter one, you would have an index page in a folder named Username, but I don't know how to do it dynamically and to show it as a subfolder.
I am using php as my main language.
You can achieve friendly urls using Apache's mod rewrite. Just enable it in Apache config and put the following to .htaccess
, which will be in root of your application:
RewriteEngine On
RewriteRule /(.*)$ /profile.php?username=$1
Also, here's a nice looking tutorial regarding friendly urls.
mod_rewrite for Apache has features to implement what you want. And I'm sure other mainstream web servers have similar capabilities.
You can use htaccess, http.conf or (if you are on IIS) web.config.
I use htaccess. Here's an example of how I allow for dynamic profiles:
RewriteEngine On
RewriteBase /
RewriteRule ^profile\/(.*)$ ./profile.php?profileId=$1 [L,NC,QSA]
RewriteBase /
means that the ./profile.php?profileId=$1
path is from the root directory. And the RegExp ^profile\/(.*)$
will match something like profile/1234
resulting in displaying ./profile.php?profileId=1234
Hope that helps.