PHP : dynamic sub domains for each user using htaccess
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 )
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.
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')
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.
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.
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.
精彩评论