Question

Is there any easiest way to create a sub domain using PHP code on godaddy.com hosting?

e.g:

jon.mywebsite.com
jessie.mywebsite.com

etc

Was it helpful?

Solution

you need to create a A record to serve all your subdomains

*.your-site.com       IN  A       YOUR-IP-ADDRESS

then you need to create a .htaccess file and

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

this will ignore images (SEO friendly URLs).

Now you can redirect your users to $userName.your-site.com

Alternatively try this:

setup your application so your users goes to your-site.com/user your .htaccess should look like this

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([aA-zZ])$  index.php?username=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.mywebsite.com
RewriteRule (.*)  index.php?username=%1

so now when you hit the index.php it will grab the user and redirect to $user.your-site.com as a custom subdomain. (in this case usernames are limited to a-z characters)

OTHER TIPS

I dont think with PHP Code you can but with the help of .htaccess i think it is possible.

RewriteEngine On

RewriteCond %{THE_REQUEST} \ /+jon/
RewriteRule ^jon/(.*)$ http://jon.mywebsite.com/$1 [L,R=301]

RewriteCond %{THE_REQUEST} \ /+jessie/
RewriteRule ^jessie/(.*)$ http://jessie.mywebsite.com/$1 [L,R=301]

RewriteRule ^(jon|jessie)/ - [L]

RewriteCond %{HTTP_HOST} ^(www\.)?jon\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ /jon/$1 [L]

RewriteCond %{HTTP_HOST} ^(www\.)?jessie\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ /jessie/$1 [L]

You can make subdomain much easier using control panel, why to do it the hard way ?

This is an old question, but in case anyone is looking for the answer. This is specific to GoDaddy, but other Hosts may do something similar to this.

After you setup your DNS A record you need to go to Hosted Domains and add the subdomain folder name under the root domain account. Go to Hosted Domains. Under Hosted Domains you will see a root domain table column. You will also see a Subdomains table column with an "Add" link. This link is for adding subdomains to a root domain. Type in/enter the name of the subdomain folder.

Create either an index.php or index.html file and upload it to your subdomain folder and echo something like "Setup Completed". I noticed that if you are doing something like creating a restricted "api" subdomain for example and the GD script cannot access the index file in your subdomain folder then the "process" does not complete until you add a plain index.php or index.html file in your subdomain folder. After that is done then you can add anything you want in that subdomain folder/site.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top