tags:

views:

7192

answers:

10

Hi,

How do I create subdomain like http://user.mywebsite.com ? Do i have to access htaccess somehow? Is it actually simply possible to create it via pure php code or I need to use some external script-server side language?

To those who answered: Well, then, should i ask my hosting if they provide some sort of DNS access??

+1  A: 

In addition to configuration changes on your WWW server to handle the new subdomain, your code would need to be making changes to your DNS records. So, unless you're running your own BIND (or similar), you'll need to figure out how to access your name server provider's configuration. If they don't offer some sort of API, this might get tricky.

Update: yes, I would check with your registrar if they're also providing the name server service (as is often the case). I've never explored this option before but I suspect most of the consumer registrars do not. I Googled for GoDaddy APIs and GoDaddy DNS APIs but wasn't able to turn anything up, so I guess the best option would be to check out the online help with your provider, and if that doesn't answer the question, get a hold of their support staff.

theraccoonbear
+1  A: 

You could [potentially] do a rewrite of the URL, but yes: you have to have control of your DNS settings so that when a user is added it gets its own subdomain.

warren
+17  A: 

You're looking to create a custom A record.

I'm pretty sure that you can use wildcards when specifying A records which would let you do something like this:

*.mywebsite.com       IN  A       127.0.0.1

127.0.0.1 would be the IP address of your webserver. The method of actually adding the record will depend on your host.


Doing it like http://mywebsite.com/user would be a lot easier to set up if it's an option.

Then you could just add a .htaccess file that looks like this:

Options +FollowSymLinks

RewriteEngine On
RewriteRule ^([aA-zZ])$  dostuff.php?username=$1

In the above, usernames are limited to the characters a-z


The rewrite rule for grabbing the subdomain would look like this:

RewriteCond %{HTTP_HOST} ^(^.*)\.mywebsite.com
RewriteRule (.*)  dostuff.php?username=%1
Mark Biek
I've offered the second option to customer, but he prefers the subdomai - it really looks better though
Skuta
Unless each subdomain is served off of a different server, I would thing creating Canonical Name (CNAME) records would be a better plan.http://en.wikipedia.org/wiki/List_of_DNS_record_types
theraccoonbear
The subdomain does look nice. It's going to be a lot more work to set up though because you'll have to make sure that all subdomain requests get handled properly. This isn't terrible with Apache but might be tricky in a hosted environment.
Mark Biek
@theraccoonbear Assuming you can use wildcards with CNAME as well, that's a thought.
Mark Biek
Does it actually mean that PHP code would have to add a new Rewriterule to htacccess only?
Skuta
If you get the DNS and rewrite rules set up properly, you'll just be passing whatever the current username is into PHP as a GET variable. Then you can handle it from there however you want.
Mark Biek
PHP shouldn't have to worry about adding anything. It just deals with what gets fed to it.
Mark Biek
Mark, I tried to put that "http://mywebsite.com/user into .htaccess however the website did not display. I guess something went wrong with hosting ^^ Some "IIS Password" problem.
Skuta
I should have been more specific. .htaccess is for Apache, not IIS. I'm not sure how url rewriting is handled with IIS but I believe there have been some questions about there on SO.
Mark Biek
I've contacted hosting, they said I got to pay some little fee to transfer web from IIS to Linux server ^^
Skuta
+1  A: 

If you use Cpanel, this is a very good tutorial.

Gaurav
A: 

I do it a little different from Mark. I pass the entire domain and grab the subdomain in php.

RewriteCond {REQUEST_URI} !\.(png|gif|jpg)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?uri=$1&hostName=%{HTTP_HOST}

This ignores images and maps everything else to my index.php file. So if I go to

http://fred.mywebsite.com/album/Dance/now

I get back

http://fred.mywebsite.com/index.php?uri=album/Dance/now&hostName=fred.mywebsite.com

Then in my index.php code i just explode my username off of the hostName. This gives me nice pretty SEO URLs.

gradbot
+1  A: 

Don't fuss around with .htaccess files when you can use Apache mass virtual hosting.

From the documentation:

#include part of the server name in the filenames VirtualDocumentRoot /www/hosts/%2/docs

In a way it's the reverse of your question: every 'subdomain' is a user. If the user does not exist, you get an 404.

The only drawback is that the environment variable DOCUMENT_ROOT is not correctly set to the used subdirectory, but the default document_root in de htconfig.

Wimmer
I am not really sure how to use that.. and in the end, why not to mess up with htaccess?
Skuta
A: 

We setup wildcard DNS like they explained above. So the a record is *.yourname.com

Then all of the subdomains are actually going to the same place, but php treats each subdomain as a different account.

We use the following code: $url=$_SERVER["REQUEST_URL"]; $account=str_replace(".yourdomain.com","",$url);

This code just sets the $account variable the same as the subdomain. You could then retrieve their files and other information based on their account.

This probably isn't as efficient as the ways they list above, but if you don't have access to BIND and/or limited .htaccess this method should work (as long as your host will setup the wildcard for you).

We actually use this method to connect to the customers database for a multi-company e-commerce application, but it may work for you as well.

A: 

Hi,

In case you have a cpanel based account, then you can easily create subdomain using php. Here is the link that shows how to create subdomain using PHP.

http://php-tutor.com/creating-subdomains-automatically/

This site has more quick php tutorials to various other whm and cpanel functionalies so you can check them out too...

Om Technologies
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
Why the down vote?
balupton
A: 

wo, is there a way to create subdomains only through a php file ?

like create a function call createsubdomain($username) or something?

that would really be helpful in the sense that as soon as a user registers himself on a website, he gets to access hisname.thewebsite.com to access his full profile

thanks for replues

equatorlounge.wordpress.com

equatorlounge