Can this be done? site.com/home.php?id=user -->user.site.

Using a single web hosting account to host multiple sites

Can this be done? site.com/home.php?id=user -->user.site.

Postby lh03 » Fri Sep 29, 2006 6:24 pm

I have a member site and need to give the users a simple URL for their portion of the site.
The members' sites are at:
www.site.com/home.php?userid=1 (depends on their userid).

The site is currently set up so the members can use:
http://shops.site.com/username

However, this url changes to:
www.site.com/home.php?id=1
through a redirect

I want to keep the member's username in the url. It would be nice to have the following url scheme:
http://username.site.com/page.php

Is this possible? If so, how? I'm a newbie to mod_rewrite.
lh03
 
Posts: 5
Joined: Fri Sep 29, 2006 6:09 pm

Postby richardk » Sat Sep 30, 2006 8:39 am

How is the current redirect done? You'll probably need more than mod_rewrite for a Username to ID conversion.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby lh03 » Sat Sep 30, 2006 4:31 pm

This url:
http://shops.site.com/username
leads to:
shops/username/index.php
which contains the following code:
header ("Location: http://www.site.com/home.php?userid=1")

This process can be changed in order to work with mod_rewrite.

Just as a reiteration of what I'm trying to figure out:
I need the url to appear like:
http://username.site.com/home.php

and behind the scenes the url is really:
www.site.com/home.php?userid=1

Thanks again!
lh03
 
Posts: 5
Joined: Fri Sep 29, 2006 6:09 pm

Postby richardk » Sun Oct 01, 2006 9:02 am

You need DNS for the subdomains.
The subdomains need to point to your main document root.

Then you could use something like this:
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^/]+)\.domain\.com$ [NC]
RewriteRule !^modrewrite\.php$ /mod_rewrite.php?mr_user=%2 [QSA,L]


In modrewrite.php:
Code: Select all
<?php

$mr_name2id = array(
  'username-one' => 1,
  'username-two' => 2,
  // ...
  'username-abc' => 123,
  );

if(array_key_exists($mr_name2id[$_GET['mr_user']]))
{
  $_GET['userid'] = $mr_name2id[$_GET['mr_user']];
  $mr_uri = explode('?', getenv('REQUEST_URI'));
  unset($_GET['mr_user'], $mr_name2id);
  include(getenv('DOCUMENT_ROOT') . $mr_uri[0]);
}
else
{
  // no username
  $mr_uri = explode('?', getenv('REQUEST_URI'));
  header('Location: http://' . $getenv('HTTP_HOST') . $mr_uri[0], true, 301);
  exit();
}


You wouldn't need the current directories. It has an array at the top for the username to userid conversion, but you could do that with files or a database.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby lh03 » Sun Oct 01, 2006 4:40 pm

Thanks! I'll try it out in the morning and let you know how it goes.
lh03
 
Posts: 5
Joined: Fri Sep 29, 2006 6:09 pm

Postby lh03 » Mon Oct 02, 2006 4:21 pm

I'm not a pro with arrays and spent an 1 1/2 hours trying to figure this out. Would you mind helping? I am going to pull the username and id from the database. How do I place them into an array like the one you showed:

$i=0;
$getUsers = mysql_query("SELECT DISTINCT ownerid, site_name FROM site_users");
while ($userArray = mysql_fetch_array($getUsers)) {
$sitename[$i]=$userArray['site_name'];
$ownerid[$i]=$userArray['ownerid'];
$i++;
}

$mr_name2id = array(
'username-one' => 1,
'username-two' => 2,
// ...
'username-abc' => 123,
);

Also, do I need to create a subdomain for each user? I'm not sure what you meant in this statement:

"You need DNS for the subdomains.
The subdomains need to point to your main document root."

I very much appreciate the time you've given to help!
lh03
 
Posts: 5
Joined: Fri Sep 29, 2006 6:09 pm

Postby richardk » Tue Oct 03, 2006 8:44 am

Also, do I need to create a subdomain for each user? I'm not sure what you meant in this statement:

"You need DNS for the subdomains.

Each user needs a subdoamin created, or you can have Wildcard DNS where any subdoamin will work.

The subdomains need to point to your main document root."

When you go to the subdomains (without any mod_rewrite), they need to go to the same files as your main domain does.

am going to pull the username and id from the database.

If you're doing a database query you don't need the array, something like this:
Code: Select all
<?php

# check for username
if(!isset($_GET['mr_user']))
{
  header('Location: http://' . getenv('HTTP_HOST') . '/', true, 301);
  exit();
}

# connect to database server
if(!($mr['con'] = mysql_connect('localhost', 'mysql_user', 'mysql_password')))
{
  exit('Could not connect to MySQL server: ' . mysql_error());
}

# select database
if(!mysql_select_db('foo', $mr['con']))
{
  exit('Could not select database: ' . mysql_error($mr['con']));
}

# clean the input
$mr['un'] = mysql_real_escape_string(trim($_GET['mr_user']));
unset($_GET['mr_user'])

# search for user's ID
if(!($mr['res'] = mysql_query('SELECT ownerid FROM site_users WHERE sitename="'
     . $mr['un'] . '" LIMIT 1', $mr['con'])))
{
  exit('MySQL query failed: ' . mysql_error());
}

# put user's ID in the userid GET variable
$mr['res2'] = mysql_fetch_assoc($mr['res']);

# no userid
if(empty($mr['res2']))
{
  header('Location: http://' . getenv('HTTP_HOST') . '/', true, 301);
  exit();
}
$_GET['userid'] =  $mr['res']['ownerid'];

# get the filename
$mr_uri = explode('?', getenv('REQUEST_URI'));
unset($_GET['mr_user'], $mr);

# include the file
include(getenv('DOCUMENT_ROOT') . $mr_uri[0]);

It isn't tested though, but it's the basic idea.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby lh03 » Thu Oct 05, 2006 7:52 am

Thank you for helping. I haven't had time to get back to it, but plan to on Friday.
lh03
 
Posts: 5
Joined: Fri Sep 29, 2006 6:09 pm


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 38 guests

cron