Asked  7 Months ago    Answers:  5   Viewed   75 times

I am looking to create a system which on signup will create a subdomain on my website for the users account area.


I think it would be something to do with the .htaccess file and possibly redirecting to another location on the website? I don't actually know. But any information to start me off would be greatly appreciated.

Creating a sign up area is not the problem - I have done this many a time. I am just unsure where to start with the subdomain.



The quick rundown

  1. You need to create a wildcard domain on your DNS server *
  2. Then in your vhost container you will need to specify the wildcard as well * - This is done in the ServerAlias DOCs
  3. Then extract and verify the subdomain in PHP and display the appropriate data

The long version

1. Create a wildcard DNS entry

In your DNS settings you need to create a wildcard domain entry such as * A wildcard entry looks like this:

*   3600  A

2. Include the wildcard in vhost

Next up in the Apache configuration you need to set up a vhost container that specifies the wildcard in the ServerAlias DOCs directive. An example vhost container:

<VirtualHost *:80>
  ServerAlias *
  UseCanonicalName Off

3. Work out which subdomain you are on in PHP

Then in your PHP scripts you can find out the domain by looking in the $_SERVER super global variable. Here is an example of grabbing the subdomain in PHP:

preg_match('/([^.]+)', $_SERVER['SERVER_NAME'], $matches);
if(isset($matches[1])) {
    $subdomain = $matches[1];

I have used regex here to to allow for people hitting your site via or

If you never anticipate having to deal with www. (or other subdomains) then you could simply use a substring like so:

$subdomain = substr(
                 $_SERVER['SERVER_NAME'], 0,
                 strpos($_SERVER['SERVER_NAME'], '.')

Mass Virtual Hosting

Mass virtual hosting is a slightly different scheme to the above in that you would usually use it to host many distinct websites rather than attempting to use it power an application as the question proposes.

I have documented my mod_rewrite based mass virtual hosting environment before in a post on my blog, which you could look at if that is the route you wish to take. There is also, of course, the respective Apache manual page.

Apache also has an internal way of dealing with mass virtual hosting that is slightly less flexible than the mod_rewrite method I have used. This is all described on the Apache Dynamically Configured Mass Virtual Hosting manual page.

Wednesday, March 31, 2021
answered 7 Months ago

Save thumbnails to disk. Image processing takes a lot of resources and, depending on the size of the image, might exceed the default allowed memory limit for php. It is less of a concern if you have your own server with only your application running but it still takes a lot of cpu power and memory to resize images. If you're considering creating thumbnails on the fly anyway, you don't have to change much - upon the first request, create the thumbnail from the source file, save it to disk and upon subsequent requests just read it off the disk.

Wednesday, March 31, 2021
answered 7 Months ago

Using mod_rewrite, you can try:

RewriteEngine On

# the request URI doesn't already start with /users/
RewriteCond %{REQUEST_URI} !^/users/

# host doesn't start with www
RewriteCond %{HTTP_HOST} !^www.  [NC]

# host starts with something else
RewriteCond %{HTTP_HOST} ^([^.]+)$  [NC]

# rewrite
RewriteRule ^(.*)$ /users/%1/$1  [L]

This will make it so when someone enters they will be served the file in /users/joe/some/page.html

Tuesday, August 3, 2021
answered 3 Months ago

You can do:

var routeSelect = $("#PAG_POSITION").get(0);

routeSelect.html(''); //clear-out options

if (isNaN(opts) || opts == 0) {
    //Handles case where your response is invalid or zero
} else {
    //Add n items to the dropdown
    for(var i = 0; i < opts; ++i) {

Hope this helps.

Wednesday, August 25, 2021
Rafael Romão
answered 2 Months ago

I tried everything and nobody could help me. After much research, I found this and it works for me. So here my own answer, that may help others searching for the same thing.

This will make that the URL showing the subdomain ("") doesn't change in the address bar and even if someone enters "" it will rewrite the URL to "":

RewriteEngine On

RewriteCond %{HTTP_HOST} ^$
RewriteRule ^en(/(.*))?$2 [QSA,L,R=301]

This will break the paths of your site, causing that styles and images won't show. Therefore you need to put this in your HTML code on every page of your site, according to the location of each page in the structure of your site:

For the page in folder "en":

<base href="" />

For the page in folder "aaa":

<base href="" />

For the page in folder "bbb":

<base href="" />

You are welcome! :-)

Wednesday, August 25, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :