On one ip hanging several sites. The task is this: you need from any non-existent subdomain (123123.site1.com, or qwe111.site2.com, or * .site3.com) to redirect 301 to the main domain of the desired site.

111.site1.com - leads to site1.com
222.site1.com - leads to site1.com
111.site2.com - leads to site2.com
33dsd.site2.com - leads to site2.com and so on.

Now if you specify a non-existent subdomain, we fall into the default settings and error 404 (htaccess in this case is not readable - AllowOverride None). If you put AllowOverride All help it in determining the target domain where to get?

How to do this and what to change in the settings so that there are no problems with the sites?

Default:

<VirtualHost *:81> ServerAdmin webmaster@localhost <Directory /> Options +FollowSymLinks Indexes AllowOverride None </Directory> <Directory /var/www/> Options Indexes +FollowSymLinks AllowOverride None Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> 

The apache2.conf file already has individual settings for each site.

    2 answers 2

    I would suggest the following option (from my point of view):

    in each virtualhost to make such approximately records:

     servername host1.ru serveralias *.host1.ru rewrite engine on rewritecond %{HTTP_HOST} !^host1\.ru$ [NC] rewriterule (.*) http://host1.ru/$1 [L,R=301,QSA] 

    but you can try to do exactly as you ask (did not check the performance).

    create a “global” virtualhost with approximately such entries in it:

     servername non.exist.ent serveralias *.host1.ru *.host2.ru *.host3.ru rewrite engine on rewritecond %{HTTP_HOST} ^([a-z0-9-]+)\.([a-z0-9-]+)\.([a-z0-9-]+)$ [NC] rewriterule (.*) %2.%3/$1 [L,R=301,QSA] 
    • rewritecond & rewriterule you have the wrong - does not work with them. I did not succeed in launching the "global" option, I implemented a local one for each individual domain. Thanks for the tip! - Prog2010
    • Yes, it is possible that I made a mistake (because I did not check the performance). if you can find it - feel free to make changes to my answer - this is the practice here. - aleksandr barakin

    I do not know whether you are suitable. I suggest you create a virtual host ( link )

    The instructions have settings for a virtual host, pay attention to serveralias

     <VirtualHost *:80> ServerAdmin admin@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> 

    in aliases and give them aliases what you need.