I'm having a bit of trouble with mod_rewrite. Thanks for any help.
I'm trying to change this starting URL:
http://www.example.com/profile.php?u=72
into this URL I want to display:
http://www.example.com/username
where username
is the username of user #72, as stored in a MySQL database. What steps should I take to make this happen f开发者_如何学JAVAor all users?
- Make sure username is unique in your database - check case sensitivity too.
- Replace all old urls in your application pointing to the former url to the one using username.
- On your user detail page, select by username instead of id.
If you have any problems, I'm happy to help with specific questions.
Sounds like you're thinking about it backwards - instead think about how you would translate example.com/username
into a URL like example.com/profile.php?name=username
, which your PHP can then work with. Usually when you use mod_rewrite, you change the links in your site to link to clean URLs, and use mod_rewrite to make it understand them.
Your mod_rewrite does not use information from a database, you use it to load another page while showing the url you want.
You cannot use it exactly like you want, if the url does not contain the user ID, you cannot redirect to http://www.example.com/profile.php?u=72
. However you can redirect to http://www.example.com/profile.php?username=username
and in profile.php
lookup the correct user based on the username instead of the id.
精彩评论