Page 1 of 1
Redirect
Posted: Sun Dec 18, 2005 9:44 pm
by Dale
I'm creating a site that involves profiles, and I was wondering how would I go about getting users to have "safe urls" for there profiles?
eg;
Username: dalehay
Profile URL:
http://www.website.com/dalehay
(Sends visitor to)
Real URL:
http://www.website.com/profile.php?name=dalehay
Any ideas?
Posted: Sun Dec 18, 2005 10:07 pm
by evilmonkey
You can use a trick such as a custom error page that will read the URL and redirect it properly. If there page really doesn't exist, then show an error message. Something like this:
Code: Select all
<?php
//error404.php
$url = $_SERVER['REQUEST_URI']; //should return "http://www.website.com/dalehay"
$profile_name = strstr("/", $url); //should return "/delehay"
//run a mysql check or something to see if a profile with such name exists
if (/*check succeeds*/){
header ("Location: http://www.website.com/profile.php?profile=dalehay");
}
else{
include ("404.php"); //this is your "not found" page
}
?>
Not sure if there's a better way of doing this, but this is a trick that sorta came to me.
Posted: Sun Dec 18, 2005 10:18 pm
by Dale
Found the solution.
(I'll post it for anyone with this problem)
.htaccessCode: Select all
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)(/)?$ profile.php?name=$1
(Make sure this is in the root directory)
Posted: Sun Dec 18, 2005 10:26 pm
by evilmonkey
Correct me if I'm worng, but I think that is mod_rewrite. Oh well, my solution is still a viable one for those that don't have mod_rewrite at all.

Posted: Sun Dec 18, 2005 10:38 pm
by Dale
Okies, thank you.