Point sub domain to a directory

Operating system: Ubuntu
OS version: 18.04

I’ve read a dozen articles on it but couldn’t figure out a simple solution to a simple problem. I have a server setup in virtualmin example.com and now I want to create a subdomain test.example.com that should point to the directory /home/example/public_html/test/. I’m not able to achieve this.

I’ve tried creating sub servers, then changing their DocumentRoot, alias servers and even tried manually. So no server for test.example.com, then added a file /etc/apache2/sites-enabled/test.example.com.conf. In it I added

<VirtualHost *:80>
   ServerName test.example.com
   Redirect 301 / https://test.example.com/

<VirtualHost *:443>
   ServerName test.example.com
   ServerAlias test.example.com
   DocumentRoot "/home/example/public_html/test"
   AccessFileName .htaccess
   <Directory "/home/example/public_html/test">
         Order allow,deny
         Allow from All
         AllowOverride All

But it doesn’t work. The domain takes me to the alphabetically first server which is what happens when a server isn’t setup. Please help.

Are you sure you want to use an asterisk (*) here? If your other VirtualHosts have a specific IP, Apache will always consider them a better match for answering the request.

Although there shouldn’t be any other hosts with this domain, I’ve changed the asterisk to my server IP just like Virtualmin generates it. It din’t make a difference though.

Would someone be able to suggest a solution to this?

Finally figured out. Enabled subdomain from System Settings > Virtualmin Configuration > Advanced options > Enable Subdomains.

I don’t recommend using cPanel-style subdomains, but if you must, I guess this does it.

Is there a better way to do it? This is the only way I was able to do it, I tried dozens of things.

I don’t recommend laying out your VirtualHosts this way at all. (There are security implications, among other things.)

What are you talking about? The code in original post?

No, I’m talking about the general concept of putting the VirtualHost DocumentRoot inside of the DocumentRoot of another VirtualHost. You might make a mistake in your htaccess or configuration that could lead to data exposure of the “subdomains” that live inside that other document root.

There’s no technical reason to do it that way, so why do it that way?

There’s no technical reason to do it that way, so why do it that way?

The reason is I couldn’t find another way.

Can you tell me a safe way to get a subdomain pointing to a directory inside public_html of the main domain? Which was my original question. Is this even possible because you keep telling me what not to do but not what to do.

No, that’s my point. Putting a DocumentRoot inside of a DocumentRoot has potential security concerns.

No matter what way you do it. What you’re trying to do is what I’m recommending you not do.

If you are going to do it, then turning on the old Subdomains feature is fine as a way to do it.

I recommend you use Sub-servers as they normally work, where every DocumentRoot is completely independent of others.

Edit: It may be fine. If you know what you’re doing, you’re aware of the potential risk, you can safely do what you want. But, the potential for mistakes is there, and there is no reason to put a DocumentRoot inside of another DocumentRoot, so…why do it at all?

Okay. I understand but in this case I must do it. The subdomain shares many files with the main domain and the file structure changes too much from dev to production environment. So relative file locations changes drastically. Its impossible to maintain code like this.

This topic was automatically closed 8 days after the last reply. New replies are no longer allowed.