Mobile app version of vmapp.org
Login or Join
Radia820

: How can I make all created directories assign to a subdomain? So we have a .php file that creates a directory that is named after user input from a form, and uses file_put_contents to have

@Radia820

Posted in: #Domains #Htaccess #Subdomain

So we have a .php file that creates a directory that is named after user input from a form, and uses file_put_contents to have a n index.php already in that directory. Our problem is that it can be only accessed by website.com/domain instead of domain.website.com. How would I automatically assign a subdomain with the same name to the directory? We have tried just about everything the internet will give us, and have been stuck for 3 hrs. We are using Apache on Debian.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Radia820

1 Comments

Sorted by latest first Latest Oldest Best

 

@Chiappetta492

You can't create a subdomain using only PHP application.

First you need to configure apache virtual host to accept requests for multiple domains.

<VirtualHost *:80>
ServerName example.com ServerAlias example.com *.example.com
DocumentRoot /www/domain
</VirtualHost>


With wildcard subdomain *.example.com, main domain will receive all requests for any sub-domain, which can be handled by either using .htaccess or with PHP application.

Now, use htaccess code to rewrite URL i.e. load subdirectory on subdomain URL,

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} =subdir.example.com
RewriteCond %{DOCUMENT_ROOT}/subdir/%{REQUEST_URI} -f
RewriteRule ^(.+) %{DOCUMENT_ROOT}/subdir/ [L]
</IfModule>


** need to write more generic htaccess code.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme