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
However, you don't really need any rewrite rules. The HTTP_HOST header is available in PHP as well, so you can get it already, like
$username = strtok($_SERVER['HTTP_HOST'], ".");
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.
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.
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_URI"];
$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.
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.
Simple PHP solution for subdomains and multi-domain web apps
Step 1. Provide DNS A record as "*" for domains (or domain) you gonna serve "example.org"
A record => *.example.org
A record => *.example.net
Step 2. Check uniquity of logins when user registering or changing login. Also, avoid dots in those logins.
Step 3. Then check the query
// Request was http://qwerty.example.org
$q = explode('.', $_SERVER['HTTP_HOST']);
/*
We get following array
Array
(
[0] => qwerty
[1] => example
[2] => org
)
*/
// Step 4.
// If second piece of array exists, request was for
// SUBDOMAIN which is stored in zero-piece $q[0]
// otherwise it was for DOMAIN
if(isset($q[2])) {
// Find stuff in database for login $q[0] or here it is "qwerty"
// Use $q[1] to check which domain is asked if u serve multiple domains
}
?>
This solution may serve different domains
qwerty.example.org
qwerty.example.net
johnsmith.somecompany.com
paulsmith.somecompany.com
If you need same nicks on different domains served differently, you may need to store user choise for domain when registering login.
smith.example.org // Show info about John Smith
smith.example.net // Show info about Paul Smith
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With