I have a database where I'm picking up information and in this case
user 1023 = madonna
I'm having today an url which is:
http://domain.com/welcome.php?p=user&xoxo=1023
and I would like to have the url like this instead
http://domain.com/user/madonna
Try to use this htaccess
Before that you should backup your original htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^xoxo=([0-9]*)$
RewriteRule ^(.*)$ http://domain.site/user/madonna [R=301,L]
It cannot be done for a arbitrary number mapping, such as one defined in a database. Instead the application itself should handle the name <-> ID/number processing in this case.
The rewrite (if used) would only be used turn something like /user/madonna
into /welcome.php?user=madonna
; likewise the 'pretty URL' should be generated by the application itself using the already-available username.
tldr; The application - not mod_rewrite - is responsible for transformation to/from the username (ie. "madonna") and the corresponding user ID (ie. 1023).
Anyway, a slightly different approach is to follow what StackOverflow does for URLs; it encodes the ID and a 'friendly' portion.
For instance a 'friendly question URL' is http//../questions/$id/$title
while the only relevant portion of the URL is http://../questions/$id
as the title portion is entirely irrelevant to locating the correct question. Similarly, my profile is http://../users/2864740/user2864740
but my username is irrelevant to processing the query and http://../users/2864740
is sufficient to access my profile.
Note: StackOverflow will immediately redirect to the correct-full-friendly-URL if the 'friendly' portion does not match the item referenced by ID; this is especially important for something like user accounts to mitigate spoofing. It also ensures the 'correct' URL is propagated after a title/username change.
In context of the question this would mean the pretty URL is http://domain.com/user/1025/madonna
were the ID is already available and easily transformed to a query parameter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.