Redirect

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Redirect

Post 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?
Last edited by Dale on Sun Dec 18, 2005 10:38 pm, edited 1 time in total.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Found the solution.

(I'll post it for anyone with this problem)

.htaccess

Code: Select all

RewriteEngine on 
RewriteRule ^([a-zA-Z0-9_-]+)(/)?$ profile.php?name=$1
(Make sure this is in the root directory)
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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. :)
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Okies, thank you.
Post Reply