Hello, I'm not sure whether this should have been in the .htaccess forum, or in here. Basically, I'm trying to create url's similar to the way myspace does it. Say a users profile is: /viewprofile.php?userid=56 And that users username is: Testaccount I'm looking for a way to be able to redirect www.mysite.com/testaccount to www.mysite.com/viewprofile.php?userid=56 If anyone has a solution, it would be appreciated much! Thanks!
This is done by using switches in php, an if and else statement may work as well but a switch is more efficient.
Edit: I managed to get something working. It involves using the 404 error page, though, which I'm not sure how it will affect the SEO of my website. Basically, I have my 404 error page using a script such as: include('../_inc/connect.php'); function curPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } $url = curPageURL(); $userurl = explode("site.com/", $url); $getId = mysql_query("SELECT * FROM SK_users WHERE username='$userurl[1]'"); $row = mysql_fetch_array($getId); $id = $row['id']; mysql_close(); Header('location: ../viewprofile.php?userid='.$id.''); PHP: When a person types something such as: www.mysite.com/Username It will search for the username 'Username' in the database, grab it's ID, and redirect properly. I should be able to come up with a way to separate error pages from user profiles, with a bit of algorithm & coding. Any suggestions from anyone? Thanks!
If you're interested in doing this an even better way (without the need for redirects, so your URL stays/looks just "www.mysite.com/testaccount" and works just like "www.mysite.com/viewprofile.php?userid=56" would): Take a look at mod_rewrite. No messy php required, does all the magic.
i don't think htaccess can access the MySQL database am i right? But what u can do is: in your htaccess, pass this url to Anyway, this is not redirect. and the url stay as www.mysite.com/testaccount The viewprofile.php file would contain the sql query that SELECT data FROM yourtable WHERE username is the $_GET['user']and display it
MOD rewrite is an apache tool.. www.yoursite.com/56 would be the it, unless you changed the id too name or whatever.