Hosting & Domaining Forum

Domain Name Discussion => Domain Beginners => Topic started by: Zora2012 on Feb 02, 2023, 04:49 AM

Title: Setting up subdomain names
Post by: Zora2012 on Feb 02, 2023, 04:49 AM
I need guidance on server settings in order to address the following tasks. Specifically, I am seeking a solution to create a third-level domain (e.g. sub1.server.com) programmatically and without restarting the server. Additionally, all requests made to sub1.server.com/* must be forwarded to sub1.server.com/index.php?request=*.

To achieve this, I have attempted to use the .htaccess file, but unfortunately, I couldn't resolve the issue of changing the browser's address bar. As it stands, accessing sub1.server.ru triggers a redirection to server.com/subs/sub1, with this path being displayed in the address bar.

It is important to note that changing the Apache settings to solve the problem of creating virtual hosts would require restarting the server, which is not an option.

As for my contribution, I would suggest exploring alternative solutions such as using a proxy server or a reverse proxy server to handle the forwarding of requests without requiring a server restart.
Title: Re: Setting up subdomains
Post by: Greenleesh on Feb 02, 2023, 05:42 AM
We can set up a redirection of browser requests to a specific server folder on the local server. For example, if we want the C:Serverdatahtdocsmp directory to be opened when typing in the browser line, and the C:Serverdatahtdocsstudent directory to be opened when http://student.work is requested.

To do this, open the file C:WindowsSystem32Driversetchosts and add the following lines:

127.0.0.1mp.loc

127.0.0.1 student.work

Save changes and close.

Next, open the C:ServerbinApache24confhttpd.conf file, and add the following code at the end:

NameVirtualHost *:80

<VirtualHost *:80>
     dоcumentRoot "c:/Server/data/htdocs"
     ServerName localhost
     ServerAlias localhost
     ErrorLog "logs/localhost.log"
     CustomLog "logs/localhost.log" common
</VirtualHost>

<VirtualHost *:80>
     dоcumentRoot "c:/Server/data/htdocs/mp"
     ServerName mp.loc
     ServerAlias mp.loc
     ErrorLog "logs/localhost.log"
     CustomLog "logs/localhost.log" common
</VirtualHost>

<VirtualHost *:80>
     dоcumentRoot "c:/Server/data/htdocs/student"
     ServerName student.work
     ServerAlias student.work
     ErrorLog "logs/localhost.log"
     CustomLog "logs/localhost.log" common
</VirtualHost>

Finally, save and close the file, and restart your computer.

As an additional suggestion, it's important to always keep backup copies of configuration files before making any changes, in case unexpected issues arise.
Title: Re: Setting up subdomains
Post by: mit_searchonic on Feb 02, 2023, 06:06 AM
To avoid having to clutter the configuration file with individual subdomains, it is possible to configure Apache to allow each directory to be accessible as a subdomain. Creating a new subdomain requires only the creation of a new directory.

When setting up DNS, a new subdomain can be created in the zone description file or through settings provided by the domain registrar, by using an asterisk (*) with the IP address of the web server where all subdomains will be located. It's important to note that these "automatic" subdomains have lower priority compared to regular ones. If a subdomain with a specific name is created and pointed to a different IP address, users will be directed to that IP instead.

To set up Apache, ensure the mod_rewrite module is connected and add the following code to the VirtualHost section of your domain:

<VirtualHost *:80>
  dоcumentRoot /var/www/example.com
  ServerName example.com
  ...
  ServerAlias *.example.com
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www.example.com$
  RewriteCond %{HTTP_HOST} ^((.*)\.)example.com$
  RewriteRule ^/(.*) /%2/$1   
</VirtualHost>

With this setup, all subdomains except www will be requested from subdirectories. This configuration also allows for domains with several levels of nesting (e.g. second.first.example.com). For non-existent directories, the standard error 404 (File not found) is returned.

As an additional suggestion, it's good practice to keep thorough dоcumentation of all subdomains and their associated directories to avoid confusion and make management easier.
Title: Re: Setting up subdomain names
Post by: John on Jul 14, 2023, 04:50 AM
To create a third-level domain programmatically and without restarting the server, you can try implementing a reverse proxy server configuration using Apache's mod_proxy module. This will allow you to forward requests from sub1.server.com to sub1.server.com/index.php?request=* without restarting the server. Here's a possible solution:

1. Enable the mod_proxy module in your Apache configuration. You can do this by running the following command:

  ```
  sudo a2enmod proxy
  sudo a2enmod proxy_http
  ```

2. Create a new virtual host for sub1.server.com in your Apache configuration. This can be done by adding the following configuration to your Apache configuration file (e.g., `/etc/apache2/sites-available/sub1.server.com.conf`):

  ```
  <VirtualHost *:80>
    ServerName sub1.server.com

    <Proxy *>
      Require all granted
    </Proxy>

    ProxyPass / http://sub1.server.com/index.php?request=
    ProxyPassReverse / http://sub1.server.com/index.php?request=
  </VirtualHost>
  ```

3. Save the file and exit the editor.

4. Enable the newly created virtual host by running the following command:

  ```
  sudo a2ensite sub1.server.com.conf
  ```

5. Restart Apache for the changes to take effect:

  ```
  sudo systemctl restart apache2
  ```

With this configuration, all requests made to sub1.server.com/* will be forwarded to sub1.server.com/index.php?request=*. The browser's address bar should display sub1.server.com/* instead of server.com/subs/sub1.

Please note that the exact steps may vary depending on your server setup and Apache version. It's recommended to consult the Apache dоcumentation for more information on configuring reverse proxy servers.
Title: Re: Setting up subdomain names
Post by: qcowazhi on Jun 02, 2025, 10:02 AM
You can dynamically add subdomain configs to the proxy layer using a script (think Python or Bash) to update the proxy conf and reload it with a soft "reload" command - zero downtime, pure magic.
For routing all requests to sub1.server.com/index.php?request=, use a rewrite rule in the proxy or a lightweight .htaccess hack with RewriteRule ^(.) index.php?request=\1 [L,QSA] to keep the URL clean in the browser - no ugly redirects.

This sidesteps the address bar mess you mentioned. If your stack's already got Apache, layer Nginx as a reverse proxy to handle the subdomain routing. It's a slick workaround, and you won't need to touch the main server's guts.