.htaccess convirtiendo id a usuario a través de la base de datos mySQL

I'm looking to use mod_rewrite to mask user profiles on my site. However, their profiles are decided by their id in the format /profile.php?id=1. Both 'user' and 'id' are in the mySQL table 'users'. Is there a way to reroute the URL to read /user? Sorry if that's badly explained!

preguntado el 08 de enero de 11 a las 17:01

3 Respuestas

RewriteEngine on
RewriteRule ^profile/([^/\.]+)/?$ /profile.php?user=$1 [L]

This will cause all /profile/username to be redirected to profile.php?user=username. You can then check in your PHP:

if (isset($_GET['user'])) {
  // Check if the username exists.
  // SELECT ... FROM users WHERE username = ..
}

Respondido el 08 de enero de 11 a las 20:01

Thanks for the reply, that's almost what I'm looking for. How would I be able to do it so that if the page is www.domain.com/profile?id=1 to reroute it to www.domain.com/user where this user's id=1? - Sebastián

In PHP, simply find the user by ID, and use header('location: ') to redirect him to the url. - Christian Joudrey

.htaccess will not be able to use the info from the database to do what you desire. Even if it could you shouldn't do it :).

But what you can do is to change the profile.php to be able to read an user by the name. Let's say to make profile.php able to process a new parameter name= and you add logic in it to look in the database either via an id or via the name. If you have this then is actually trivial to route / to /profile.php?name=.

You will also have to change the website generation to output proper urls.

Also look at Cristian's answer for more technical details. It's the same idea in the end.

Respondido el 08 de enero de 11 a las 20:01

You wouldnt change this in htaccess, or with any kind of config/rewrite rule. This would be done in your application PHP if I understand what you want.

in /profile.php?id=1 profile.php is (rewrite rules aside) the source PHP file, and id is a GET parameter. Its like an argument to profile.php when it executed. They come in the form source.php?get1=val1?get2=val2.... and you access them through the array $_GET

If you don't want users to see that id, you can do one of two things.

  1. Replace it with user, as you suggest. URL would be 'profile.php?username=XXX'

Then in your code you will presumably need to perform a simple database query to get the id from the user name. Should be about 5 lines towards the top of the code in profile.php and the rest can remain unchanged Beware that this demands unique user names in case you werent before, and some special characters might be a problem with get parameters in the URL? For most user names, uniqueness is required and special characters are not allowed, so hopefully this isnt a problem.

  1. Keep id but make it a POST peremeters. URL would be 'profile.php' POST parameters are similar to get but they are not visible to the user because they aren part of the URL string. Just switch $_GET to $_POST in profile.php. The code will still identify users by id, but they won't be able to see their IDs.

In either situation you will need to change any incoming links.actions that lead to the descrubed URL that we are changing.

EDIT - Christian's has a good suggestion which is similar to #1 above but would allow for nicer looking URLs like profile/XXX rather than 'profile.php?username=XXX'. Code within profile.php would ne hte same, his changes are in profile.php son lo mismo

Respondido el 08 de enero de 11 a las 20:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.