2

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!

3
  • 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. Commented Apr 24, 2014 at 17:36
  • Thank you @mseifert for the response. Is this similar to the way StackOverflow does it with their question URLs? Commented Apr 24, 2014 at 17:37
  • I've responded in answer format. See below. Commented Apr 24, 2014 at 18:20

2 Answers 2

2

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"

Sign up to request clarification or add additional context in comments.

4 Comments

Is this how most big sites do it? I want to minimize the amount of loading required to process a URL. Also, how could I make sure that these links are indexed by search engines, i.e. does this method allow me to display all the users? Ideally I'd like the URL with the user's name to turn up in search results if someone searched for it. I appreciate your help!
The search engines see only the output, generated by the script, so they treat "site.com/johnny95" as a separate page. To help them index your site, construct a site map (you can generate it with php), robots.txt, add relevant meta tags and so on.
And for the most big sites - probbably depends on the site, but the principle is this - take whatever url the user typed, and depending on it construct a page (edit: in some rare cases they may redirect a browser to different url).
This is exactly what I was looking for!! Thank you!!
0

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.

1 Comment

I can see how they would go about pulling the relevant details using the key ID, but how would they allow the search-engine indexing of these questions with the question title included? Do they just cache all the questions? Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.