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.

e.g. johndoe.website.com

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.

 Answers

86

The quick rundown

  1. You need to create a wildcard domain on your DNS server *.website.com
  2. Then in your vhost container you will need to specify the wildcard as well *.website.com - 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 *.example.org. A wildcard entry looks like this:

*.example.org.   3600  A  127.0.0.1

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>
  ServerName server.example.org
  ServerAlias *.example.org
  UseCanonicalName Off
</VirtualHost>

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('/([^.]+).example.org/', $_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 www.subdomain.example.org or subdomain.example.org.

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
 
koenHuybrechts
answered 7 Months ago
44

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
 
smiggle
answered 7 Months ago
78

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} ^([^.]+).mywebsite.com$  [NC]

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

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

Tuesday, August 3, 2021
 
Slinky
answered 3 Months ago
25

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
    routeSelect.append($('<option/>').val(0).html(0));
} else {
    //Add n items to the dropdown
    for(var i = 0; i < opts; ++i) {
        routeSelect.append($('<option/>').val(i).html(i));
    }
}

Hope this helps.

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

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 ("en.domain.com") doesn't change in the address bar and even if someone enters "domain.com/en/" it will rewrite the URL to "en.domain.com":

RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^en(/(.*))? http://en.domain.com/$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":

<head>
<base href="http://domain.com/en/" />
</head>

For the page in folder "aaa":

<head>
<base href="http://domain.com/en/aaa/" />
</head>

For the page in folder "bbb":

<head>
<base href="http://domain.com/en/aaa/bbb/" />
</head>

You are welcome! :-)

Wednesday, August 25, 2021
 
maelgrove
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 :