Web Develop Forum: Creating dynamic subdomains. - Web Develop Forum

Jump to content

Sign In    Register    Help

Hello Guest!
  • You cannot start a new topic
  • You cannot reply to this topic
Page 1 of 1

  Creating dynamic subdomains. Rate Topic: -----

#1 User is offline   Sam Granger Icon

  • Administrator
  • PipPipPip
  • Group: Root Admin
  • Posts: 431
  • Joined: 22-October 08

Post icon  Posted 27 October 2008 - 02:48 PM

Thought I'd share this code with you all since it took me a long time to figure out. <img src="http://www.webdevelopforum.com/public/style_emoticons/<#EMO_DIR#>/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

<b>You need a host with wildcard subdomains turned on for this to work. Contact your host if this isn't already done, it isn't a hard setting to change.</b>

Options +FollowSymlinks
RewriteEngine On

Options -MultiViews

RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.yourdomain\.com
RewriteRule (.*) _controller.php?username=%1&module=$1 [L,QSA]


Replace yourdomain with your domain name. The subdomain displays what ?username= returns so if you go to example.domain.com it displays _controller.php?username=example - however it doesnt show this, appears as the subdomain still. Going to example.domain.com/hi displays _controller.php?username=example&module=hi. If you bump into any problems, post here and I'll look into it :).

Enjoy!
0

#2 User is offline   paulOr Icon

  • Member
  • PipPip
  • Group: Members
  • Posts: 20
  • Joined: 23-October 08
  • Location:Edinburgh, Scotland

Posted 02 November 2008 - 11:24 PM

ooooo iv been looking for something like this for along time! Thank you very much Sam
0

#3 User is offline   Sam Asante Icon

  • Advanced Member
  • Icon
  • Group: Moderator
  • Posts: 66
  • Joined: 18-January 09
  • Gender:Male
  • Location:United Kingdom

Posted 18 January 2009 - 10:30 AM

Will try it sometime.
Can you recommend a host with Wildcard Subdomains turned on (and good uptime)?
0

#4 User is offline   Sam Granger Icon

  • Administrator
  • PipPipPip
  • Group: Root Admin
  • Posts: 431
  • Joined: 22-October 08

Posted 18 January 2009 - 07:26 PM

Sensehost enabled it for me no problem. Most hosts probably would be cool with it. Just contact them before and ask is my advice smile.gif
0

#5 User is offline   Sam Granger Icon

  • Administrator
  • PipPipPip
  • Group: Root Admin
  • Posts: 431
  • Joined: 22-October 08

Posted 21 January 2009 - 11:31 PM

See the PHP below that I used with this project. Might come in handy!

CODE
<?php

/*
* @author Sam Granger
* @build 1.0.0
* @copyright ***********.com, 2008.
*/

require ('./static/cv_header.html');
require ('./_conf.php');

// Check for requests and clean 'em up!
$username    = mysql_escape_string($_GET['username']);    // Oi, so which profile are we trying to view then ey?
$module        = mysql_escape_string($_GET['module']);        // So you want to view a section of this profile then too?

// Begin getting all the contents to display!
$connection = new mysqli($data_server, $data_user, $data_password, $data_database);

if ($result = $connection->query("SELECT id, username, fullname FROM cv_users WHERE username = '$username'")) {

    while ($row = $result->fetch_object()) {
        echo '<h3><a href="http://'.$row->username.'.castverse.com">'.$row->fullname.'</a></h3>';
        echo '<p>Viewing: '.$module.'</p>';
    }

} else {
    echo "This user ($username) doesn't appear to exist.";
}

$connection->close();
// End getting all the contents to display!

require ('./static/cv_footer.html');

?>

0

#6 User is offline   Traceur1993 Icon

  • Advanced Member
  • Icon
  • Group: Moderator
  • Posts: 279
  • Joined: 26-January 09
  • Gender:Male
  • Location:USA
  • Interests:Parkour, Freerunning, Web Development, Reading, Writing, Computers in general...

Posted 26 January 2009 - 05:22 PM

Thanks for sharing this code.
Want to report something to a moderator? Use the 'report' button located on every post.
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users