views:

126

answers:

6

Hi guys I'm setting up mywebapplication to give unique urls for users such as uniquename.mysite.com, anotheuniqname.mysite.com etc.

I want to be able to in a php script grab the subdomain part of the url. Would be nice if I can get it as a GET variable - I think it can be done with htaccess. Any ideas?

+1  A: 

Ali - don't you mean a $_SERVER variable as $_GET would be related to the querystring ??

if so, then you could try:

$_SERVER['HTTP_HOST'] 

jim

[edit] - see http://roshanbh.com.np/2008/05/useful-server-variables-php.html for a few examples that may help

jim
Actually I'm set up my web application to be accessed from set urls as users set up. Like mysite.wordpress.com.
Ali
thanks anax - forgot the 4 spaces re code :)
jim
+2  A: 

I would try this (at the beginning of your PHP code):

$_GET['subdomain'] = substr($_SERVER['SERVER_NAME'], 0, strrpos($_SERVER['SERVER_NAME'], '.'));

Explanation: strrpos find the last occurance of '.' in the accessed domain, substr then returns the string up to this position. I assigned the return value to a $_GET var so you can use it as if it were a normal URL-parameter.

nikic
+1  A: 
$subdomain = substr($_SERVER['HTTP_HOST'], 0, strpos($_SERVER['HTTP_HOST'], '.'));

To make sure there's a valid subdomain:

$urlExplode = explode('.', $_SERVER['HTTP_HOST']);

if (count($urlExplode) > 2 && $urlExplode[0] !== 'www') {

    $subdomain = $urlExplode[0];

}
else {
    // treat as www.mysite.com or mysite.com
}
robdog
Nice but how would I check to make sure the url has a subdomain ? if he just types in mysite.com without a subdomain the code would return mysite as though it were a subdomain.
Ali
See my edit above. Checks theres are more than 2 dots and that the first term isn't www.
robdog
+2  A: 

Put this in your bootstrap file:

$tmp = explode(".", $_SERVER["HTTP_HOST"], 2);
$_GET["subdomain"] = $tmp[0];
Maerlyn
I wonder whether you could write `list($_GET['subdomain']) = explode(".", $_SERVER["HTTP_HOST"], 2);` But still, the substr + strpos variant is better ;)
nikic
@nikic you cannot, as explode returns more, than just the subdomain, also returnes the rest of the string, so you'll need some temporary variable using this method anyway
Maerlyn
A: 

If you really want to use mod_rewrite in your .htaccess file, you could do something like this (assuming your script is index.php in this example):

RewriteEngine On

# Prevent people from going to to index.php directly and spoofing the
# subdomain value, since this may not be a desirable action
RewriteCond %{THE_REQUEST} !^[A-Z]+\sindex\.php.*(?|&)subdomain=
RewriteRule ^.*$ http://%{HTTP_HOST}/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST}        !^www
RewriteCond %{HTTP_HOST}         ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^.*$ index.php?subdomain=%1
Tim Stone
If you had that in your `httpd.conf` file and not an `.htaccess` file, then I don't see why PHP would be more efficient. Since PHP is interpreted, and most web servers are compiled, I think `mod_rewrite` would be more efficient.
icktoofay
I'm inclined to agree with you. Assuming he were to do it in a per-directory context though (based on his wording), and given that he's going to be invoking the PHP script either way, I'm not sure whether `mod_rewrite`'s expression compiling and the internal redirection it's forced to make in that phase are going to be quicker than just processing the variables in the script. I imagine the difference isn't very significant anyway.
Tim Stone
A: 

Do you want something along the lines of:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[a-z0-9-]+\.yoursite\.com$
RewriteRule !^index\.php$ index.php [L]

you could then get the subdomain using

<?php

preg_match('/^http:\/\/([a-z0-9-]+)\.yoursite.com$', $_SERVER['HTTP_HOST'], $matches);

$_GET['username'] = $matches[1];

?>
Kieran Allen