I want to do a redirection where a user enters their user name in the URL and redirects to their profile, like this:

example.com/joe redirects example.com/profile.php?username=joe

I want to do it like Facebook does

Recommended Answers

All 14 Replies

Sorry, haven't coded PHP for a long time. But this is basicalyy how I did it:
1) set up a 404-page in your .htaccess-file (don't know right now without looking it up, but there's plenty of stuff on the net)
2) you 404-file reads the full URL with the $_PHP-something-variables
3) redirect with header() to the php-file with the argument from the URL
I bet facebook and so on don't do it differently

Hi,

I'm a bit confused in that I'm not sure which way you want to go.

If you have an Apache web server, you're going to want to create an .htaccess file in your document root and then use 301 redirects. The file should say something like this:

RewriteEngine on

// To go *FROM* example.com/joe TO example.com/profile.php?username=joe
RewriteRule ^([A-Za-z0-9]+)$ http://www.example.com/profile.php?username=$1 [R=301,L]

// To go *FROM* example.com/profile.php?username=joe TO example.com/joe
RewriteCond %{QUERY_STRING} ^username=([A-Za-z0-9]+)$ 
RewriteRule ^profile.php$ http://www.example.com/%1 [R=301,L]

Please note I'm just doing this from memory so it might not work.

If you want to do an 'internal' rewrite, you can remove the R=301 part of the example above and the browser will show the /joe URL while your web application will see profile.php?username=joe as the requested URL.

Or you could just use php direct. Get the http referer link the user types on your pages, if it contains example.com/user then redirect.
\

 $url=$_HTTP['REQUEST_URI'];
 $match=preg_match('|^example\.com\/[a-zA-Z0-9]+ $|i',$url,$username);
 ///or u can replace the + there wif this {max username chars iistin from 1. }
 ///like this {2,3,4,5,6} for 6 chars as max
 if($match){&varpush=var_dump($username);
  &user=explode('/',$username[0]);
  ///check ifthere are no extra slashes
 if(sizeof($username) >2){//tel the the user that he has entered an invalid name}
 else{$realusername=$username[1];}

\
goodluck

i used this rule

RewriteRule ^([A-Za-z0-9]+)$ profile.php?username=$1 [L]

its working but it was not working for when i click logout which was example.com/logout it's not working for that means its not redirecting to logout page.

Member Avatar for diafol

Your problem is that Apache won't know which page to serve with just a single parameter, as in your case, as it will be looking for an user called logout. You could preceed the parameter with an indentifier as in:

www.example.com/users/joe
www.example.com/logout
www.example.com/about

But

www.example.com/joe

Can be difficult unless you set php to search for users before returning the page to serve / file to include

You should try to keep the pattern consistent for each url, e.g.:

www.example.com/en/users/joe
www.example.com/en/about

domain | lang | page | action/id

Have a look at http://www.generateit.net/mod-rewrite/

@diafol, my php script took care of that. All he needs to do is when he gets the variable

 $username

he'l just pass it through a switch statement like this.

 Switch($username){
 case 'profile':
 ///redirect to profile.php; 
case 'logout':
 ///redirect to logout.php:
   ///and so on til the default case
   }
Member Avatar for diafol

Yes I understood your post Olagsfark. The point is that some pages may have similar multiple values (like profile for usernames). In this case it would be unclear which page to serve. e.g.

www.example.com/joe [users]
www.example.com/furniture [products]
www.example.com/ben [users]
www.example.com/paintings [products]

What if your username is logout? Now we have to ban usernames that are the same as pages or actions :)

Wel that's easy @diafol. The script can be modified

  <?
  function val($username){
  ///validate presence of username in db
  if(!isset($username_in_db){
  ///continue to the switch cases
  }else{
  ///use the username for anything you want
  } ?>

And for products, u just add an additional elseif statement to check the products database. @diafol, @thread creator. Its way better because it adds more flexibility and error handling incase code recieves smfin unexpected unlike .htaccess where any error is an automatic 500 error page

Member Avatar for diafol

That script does not allow you to go to page 'logout' if an user has the name logout (ok, a bit unlikely, but you never know). Also other collisions may exist, in which case the data loaded will be the first category (e.g. users in your case).

However, that aside, for a faceballs clone, it should be ok :)

Wel, the names of the page names then has to be reserved.

You will try to use this

RewriteRule ^(.*)$ profile.php?username=$1 [L]

Then in your profile.php

$username=$_GET['username'];
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$  profile.php?username=$1 [L]

</IfModule>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.