如何让用户创建一个新目录,但作为子域而不是PHP?

如何让用户创建一个新目录,但作为子域而不是PHP?

问题描述:

<?php 

$dirPath = $_POST['username'];
$dirPass = $_POST['password'];

if (file_exists($dirPath)) {
    echo("Not available");
} else {

    if (isset($_REQUEST['sav']))
    {  
        header('Location: http://example.com/'.$dirPath);
    }

    $result = mkdir($dirPath, 0755);

    function recurse_copy($src,$dst) {
        $dir = opendir($src); 
        @mkdir($dst); 
        while(false !== ( $file = readdir($dir)) ) { 
            if (( $file != '.' ) && ( $file != '..' )) { 
                if ( is_dir($src . '/' . $file) ) { 
                    recurse_copy($src . '/' . $file,$dst . '/' . $file); 
                } 
                else
                { 
                    copy($src . '/' . $file,$dst . '/' . $file); 
                } 
            } 
        } 
        closedir($dir); 
    }

    // Source directory (can be an FTP address)
    $src = "copy/";
    // Full path to the destination directory
    $dst = "$dirPath/";

    recurse_copy($src,$dst);
}
?>

Above shows the code that I am following. There is a form with an input field, when the user types something into that input and hits submit, a new directory will be created called 'Whatever he entered in the input' and it will contain all the files from an existing directory called 'Copy'.

This works perfectly. Lets say the user enters 'Example' and hits submit, he would have to go to http://example.com/Example.

How would I make it so that when the new directory is created, the user would have to go to a subdomain to access it. http://Example.example.com instead of http://example.com/Example.

I would do it this way: First configure your webserver that it routes all subdomain traffic to the same webspace. With apache you can do this with a wildcard virtual host entry. E.g.

<VirtualHost *:80>
  ServerName example.com
  ServerAlias *.example.com
  DocumentRoot /path/to/webroot/
</VirtualHost>

Second I would create a .htaccess rewrite condition that redirects subdomain requests to the matching folder.

RewriteEngine on

RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$
RewriteRule ^(.*)$ %1/$1 

I have not tested this, but I think it illustrates the concept.