I’ve searched all forums recently including StackOverflow, but couldn’t find any solution how to use .htaccess files. I’ve heard that virtual directories are created using this service.
I want the virtual path of user profile to be displayed like this if user name is set
http://www.example.com/username
or if username is not set, then user profiles will be displayed with this link
http://www.example.com/profile.php?id=134531
Just like facebook.
Can anyone post me an exact code of this? Thanks in advance.
I assume by virtual directories you mean rewrites. You have a profile.php page that loads the users profile via username or via id.
Lets take your first issue, mapping
/[username]toprofile.php?username=[username].This solution assumes that
mod_rewriteis installed on your Apache server.(Place this in a .htaccess file in the root of your webapp or in the VirtualHost directory in your Apache conf)
This will internally rewrite any request to /[username] to profile.php?username=[username].
As for using a username when available, and a id when not – You will have to implemenent that when you generate the links.
That takes care of the link building. Then you have your profile.php page:
There are loads of tutorials on URL Rewriting with Apache on the internet, just google and make your way through some test setups.