Question

My website is, in its most basic form, a network site for a specific type of individuals. When an individual registers, I would like to create a custom page (or at least the image/facade of one) displaying their profile. Currently, I am using PHP $_GET parameters to determine which profile to display when a user navigates to the respective script on the site.

Aiming for better SEO, I would like each user to have their own unique link; for example, FaceBook allows its users to have a custom URL which they can link. How can I automatically generate a page within a directory for each new user that registers, and have that page automatically update when the user updates his/her details? I understand that there may be a potential process with .htaccess, using the user's name/title/etc to pull their details from the database but displaying it as a unique URL.

Can anyone provide suggestions or examples of implementations of this? I appreciate any and all help!

P.S. I imagine that sorting the files based off of the user's location, i.e. by state, should not be that difficult once the aforementioned functionality has been sorted out. I would ideally like a directory structure such as this ../users/state/user_name, where state is the user's location, and user_name is their unique name. Thanks again!

Was it helpful?

Solution

There is little work with apache's .htaccess - most of the logic is in the php itself. Apache just redirects every request to an chosen script file.

I assume you use apache wid mod_rewrite. If that's the case you should add this to .htaccess file:

# check if mod_rewrite is present
<IfModule mod_rewrite.c>
  #turns it on
  RewriteEngine on

  #if the requested url isn't a file or a dir
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d

  #process index.php, no matter what was in the url
  RewriteRule ^ index.php [L]
</IfModule>

(note that .htaccess must also have permissions to overwrite the apache's current settings)

in the index.php you can check what url was sequested and process the info

$whatTheUserRequested=$_SERVER['REQUEST_URI'];
$parameterArray=explode('/', $whatTheUserRequested);

//check the first param.
switch($parameterArray[1])
{
  //if it was something like "thesite.com/main/fsadf/hgfdsgsdf/...."
  case "main":
    include "mainPage.php";
    //....or do something else
    break;

  //if it was something like "thesite.com/login/asdfe/xxxx/...."
  case "login":
    include "loginPage.php";
    //....or do something else
    break;

  default:
    //here you MUST check if $parameterArray[1] is a username
    if(check in db if $parameterArray[1] is user)
    {
      include "userPage.php";
    }
    else
    {
      include "pageNotFound.html"
    };
}

You may check also for the 3rd or 4th parameter in the included php's, to process url's like "mysite.com/johnny95/edit"

OTHER TIPS

You can generate the unique url based on any logic you wish. Use .htaccess to redirect sending a parameter which can queried in your database. You can find info on how here. I would stay away from directory structure based on state - people move and moving directories seems not worth the effort. Stay with a flat directory structure for simplicity

If you look at this post's url: questions/23275736/automatically-create-custom-page-or-url-for-new-site-user-php they are including "questions" (probably a database table), "23275736" (probably the table key ID) and a user friendly string. I don't know for sure, but I suspect they redirect using .htaccess in a method similar to what is in the attached link.

You can also look at this post's answer for another example which uses a redirect with a slash.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top