views:

58

answers:

5

Hello guys

is it possible to create dynamic sub domains for each user using htaccess. for example if there is a user with user name myusername, then add a sub domain for him like htttp://www.myusername.example.com, and when somebody load this page it should come as http://www.example.com/?user=myusername ( using htaccess )

Is it possible ?

Thanks

A: 

I don't believe you can do this in .htaccess files. What you're describing is configuring a number of Virtual Hosts to be served by a given instance of Apache. Virtual hosts must be configured in the Apache server config files, not the .htaccess files. See documentation.

Bill Karwin
+1  A: 

You have to tune your server software first.
?user=myusername is a path at the host www.example.com
while www.myusername.example.com is another host
A browser have to know which IP address should be called for www.myusername.example.com domain.

Thus, first of all you have to set up a DNS record to make all subdomains an aliases to main domain.
Next, you have to set up your web server software to accept all subdomains.
And finally you can process any subdomain request. no .htaccess involved.

For the details - refer to numerous same questions previously answered here on SO.

Col. Shrapnel
A: 

You can do this partially with .htaccess files, but some of the configuration will have to be done at the httpd.conf level. Basically, it'd looked like this:

a) Configure your domain to have wildcard dns for the zone. Exact details on how to do this depends on who your DNS provider is, or which BIND software you're running. But basically set things up so that *.example.com points at your server's address.
b) Configure the webserver like this:

<VirtualHost x.x.x.x:80>
    ServerName *.example.com
    ...
</VirtualHost>

<VirtualHost x.x.x.x:80>
    Server some.fixed.subdomain.example.com
    ...
</VirtualHost>

Make sure you list any non-dynamic domains AFTER the wildcard entry, or things will most likely not work. Apache's rather picky about the order this gets set up in.

With this setup, there's no need to rewrite requests into a query. You can have your scripts check $_SERVER['HTTP_HOST'] to figure out which virtual sub-domain is being served and work from there.

Marc B
A: 

Yes, It is possible!

You can use something like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).example.com [NC]
RewriteRule . http://example\.com/?user=%2/ [L]
</IfModule>

Make sure *.example.com is set to point to the server of example.com. (You will need to create a name record for the same. You have to use "Wildcard Subdomain", as pointed by 'balupton')

Vikash
this .htaccess is completely useless. Everything will work all right without it. and it has wrong syntax.
Col. Shrapnel
I don't know why you said that. I copied from one of my live site, where it works perfectly fine. Can you point out what's wrong with the syntax?
Vikash
it's wrong by design. put this .htaccess away, add this line to your index script `$_GET['user']=strtok($_SERVER['HTTP_HOST'],".");` and it will work. Work even better because subdomain will stay in the address bar
Col. Shrapnel
Agreed! It will do the same thing. However, you can do more with the htaccess.For example call some other url like: http://example.com/user/%2. (You can even do this by putting header('http://example.com/user/'.$_GET['user']) in your index.php, but I think htaccess solution is more elegant one.)
Vikash
useless redirect. nobody wants to make neat subdomain based url into ugly path based one
Col. Shrapnel
OMG, seriously? That's the exact same code, more or less that I use over at **my** site!You should accept Vikash's answer. It doens't redirect, that would be [R,L] But in order to get the Query STring, you should change it toRewriteRule . http://example\.com/?user=%2/ [L,QSA]
hopeseekr
A: 

The feature you are after is called Wildcard Subdomains. It allows you not have to setup DNS for each subdomain, and instead use apache rewrites for the redirection. You can find a nice tutorial here, but there are thousands of tutorials out there. Here is the necessary code from that tutorial:

<VirtualHost 111.22.33.55>
    DocumentRoot /www/subdomain
    ServerName www.domain.tld
    ServerAlias *.domain.tld
</VirtualHost>

However as it required the use of VirtualHosts it must be set in the server's httpd.conf file, instead of a local .htaccess.

balupton