Dynamic Subdomains

Using a single web hosting account to host multiple sites

Dynamic Subdomains

Postby Krypton » Thu Jul 03, 2008 10:36 pm

Hey there

Basically ive made it so when a user registers they get thier own profile page and can access it by typing dynamicuser.domain.com instead of domain.com/user/dynamicuser.php

This is my .htaccess code

Code: Select all
RewriteEngine On

# Extract the subdomain part of domain.com
RewriteCond %{HTTP_HOST} ^([^\.]+)\.tutorialwave\.com$ [NC]

# Check that the subdomain part is not www and ftp and mail
RewriteCond %1 !^(www|ftp|mail)$ [NC]

# Redirect all requests to a php script passing as argument the subdomain
RewriteRule ^.*$ http://www.tutorialwave.com/profile.php?username=%1 [R,L]



The problem is that when the user is redirected to thier profile page it shows the URL domain.com/user/dynamicuser.php instead of dynamicuser.domain.com

Any ideas would be appreciated
Thanks :D
Krypton
 
Posts: 8
Joined: Thu Jul 03, 2008 10:25 pm

Postby richardk » Fri Jul 04, 2008 3:13 pm

Including http://www.example.com/ in the RewriteRule and using the R flag causes the redirect.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^\.]+)\.tutorialwave\.com$ [NC]
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule .* /profile.php?username=%1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Krypton » Sat Jul 05, 2008 1:27 am

ohok when i try this it comes up with 'Internet Explorer Cannot Display This Page' and the URL shows as

http://josh.tutorialwave.com/user/user/ ... r/josh.php

Also i changed the 'RewriteRule .* /profile.php?username=%1 [QSA,L]' To 'RewriteRule .* /login.php [QSA,L]' for testing purposes and none of the css or jpeg images were displayed.

Any help on this would be much appreciated!!
Krypton
 
Posts: 8
Joined: Thu Jul 03, 2008 10:25 pm

Postby richardk » Mon Jul 07, 2008 1:29 pm

/user/ does not appear in that mod_rewrite. What other mod_rewrite do you have?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Krypton » Mon Jul 07, 2008 10:48 pm

I have no other mod_rewrite but in the profile.php file i have the following code -

Code: Select all
session_start();

$username = $_GET['username'];
switch($_GET['username']){

case $username:

header("Location: /user/".strtolower($username).".php");
break;

default:

echo 'error';
break;
}


Is this the correct way to handle what im trying to acheive?
Krypton
 
Posts: 8
Joined: Thu Jul 03, 2008 10:25 pm

Postby richardk » Wed Jul 09, 2008 1:41 pm

What are you trying to do? If you're trying to make the username lowercase only, try
Code: Select all
<?php

session_start();

// Check if the username is lowercase.
if(strtolower($_GET['username']) !== $_GET['username'])
{
  // It's not. Redirect.
  header('Location: /user/' . strtolower($_GET['username']) . '.php', true, 301);
  exit();
}

I don't really see how that fits with the sub domain mod_rewrite, though. If it's a sub domain it is not case sensitive. You could just lowercase it before you use it.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Krypton » Thu Jul 10, 2008 2:12 am

Sorry i havent explained it that well.

Basically when a user logs in, thier profile page is located at www.domain.com/user/dynamicuser.php, Im trying to make it so when they type in dynamicuser.domain.com it loads thier profile page instead of them having to type the full path e.g www.domain.com/user/dynamicuser.php

Also i want the URL to show dynamicuser.domain.com instead of the full path
Krypton
 
Posts: 8
Joined: Thu Jul 03, 2008 10:25 pm

Postby richardk » Sun Jul 13, 2008 10:42 am

Then you don't want to redirect to /user/, you want to redirect to the sub domain.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Krypton » Wed Jul 30, 2008 12:36 am

Then you don't want to redirect to /user/, you want to redirect to the sub domain.


Thanks for your help Richard, Sorry if i seem ignorant but what would be the easiest way to achieve this (Mod Rewrite is definitely not my cup of tea!!)
Krypton
 
Posts: 8
Joined: Thu Jul 03, 2008 10:25 pm

Postby richardk » Wed Jul 30, 2008 3:43 pm

It's part of your PHP, not mod_rewrite.

The PHP redirect from above is no longer needed, the sub domain will always be lowercase.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Next

Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 2 guests

cron