Szőke Péter
Szőke Péter

Reputation: 113

Apache URL rewriting and wildcard subdomain handling

I'm a PHP newbie and I'm creating a web app where users can register and can get a subdomain. I've already done the registration part, but I want to do one more thing which I can't figure out. So when a user browses to example.domain.com if user 'example' already registered then he should get the page from domain.com/users/example/ but if the username is not taken it should write "This domain is not registered yet. blabla"

How could I do this? thanks in advance, and sorry for my poor english :)

Upvotes: 0

Views: 934

Answers (2)

Dan
Dan

Reputation: 12096

This might be what your looking for?

RewriteEngine On
RewriteBase /   

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteRule ^$ /users/%1/ [L]

RewriteRule ^user/([a-zA-Z0-9-_]+)/$ user.php?username=$1 [L]

Then you could manage the display part within the php file and if no user associated then redirect.

You also have to add a wildcard comment to the httpd.conf file for the domain so that is supports wildcard subdomains:

ServerAlias domain.com www.domain.com
ServerAlias *.domain.com

User.php would be something like:

<?
if(mysql_num_rows(mysql_query("SELECT id FROM db WHERE username = 'mysql_real_escape_string($_GET[username])'") > 0)
{
    echo "User Exists";
}
else
{
    header("Location: /register.php?username=$_GET[username]");
}
?>

Upvotes: 3

KP-
KP-

Reputation: 11

First, create the default page that has the message "This domain is not registered yet. blabla". Call this registration.html

I am assuming you have a database storing the registered domains.

search database to see if example.domain.com is registered.

if(example.domain.com registered)
http_redirect (domain.com/users/example/);
else  
http_redirect (registration.html);

Upvotes: -1

Related Questions