I
I
ifossa2015-06-19 00:18:26
Apache HTTP Server
ifossa, 2015-06-19 00:18:26

How to make automatic subdomains in apache2?

How to configure Apache2 so that subdomains are opened from the corresponding directory
site.ru -> /var/htdocs/site.ru/www
www.site.ru -> /var/htdocs/site.ru/www
sub1.site.ru -> /var /htdocs/site.ru/sub1
subN.site.ru -> /var/htdocs/site.ru/subN

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
ifossa, 2015-06-19
@ifossa

Did it like this:

<VirtualHost *:80>
        ServerName example.ru
        ServerAlias *.example.ru

        DocumentRoot "/var/www/example.ru"

        # другие настройки #
    
        RewriteEngine on

        RewriteCond %{HTTP_HOST} ^example\.ru [NC]
        RewriteRule (.*) http://www.example.ru/$1/ [L,R=301]

        RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.ru
        RewriteCond /var/www/example.ru/%1 -d
        RewriteRule ^(.*) /%1/$1 [L]
    
</VirtualHost>

S
Sergey, 2015-06-19
@Weidmann

Add 2 directives to VirtualHost:
ServerAlias ​​*.site.com www.site.com
VirtualDocumentRoot /var/www/site.com/%1
Inside /var/www/site.com/ create a www and site symlink pointing to /var/www /site.com/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question