2

I simply want to redirect all subdomains that are not not already mentioned in a vhost's ServerName to redirect to the empty sub domain. I tried to add this to my httpd.conf after all other virtual hosts of this domain.

<VirtualHost *:80>
    ServerName *.example.com
    RedirectPermanent / http://example.com/
</VirtualHost>

The section for the empty sub domain (loaded earlier) reads like this.

<VirtualHost *:80>
    DocumentRoot /var/www/example/htdocs
    ServerName example.com
    <Directory /var/www/example/htdocs>
        Options FollowSymLinks
        AllowOverride All
        Order Allow,Deny
        Allow from all
    </Directory>
</VirtualHost>

After restarting the httpd service, I see 403 Forbidden when pointing my browser to abc.example.com. What am I doing wrong? I hoped there is no need for regex based matching as described in other answers for this simple task.

danijar
  • 403
  • 2
  • 4
  • 14

2 Answers2

7

Just add a block below the main block in your vhost configuration file. Just specify ServerAlias using the wildcard * for the subdomains. Finally, specify the redirect address using RedirectPermanent.

Listen 80
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ServerName example.com

        <Directory /var/www/html/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerAlias *.example.com
    RedirectPermanent / http://example.com/
</VirtualHost>
openwonk
  • 186
  • 1
  • 3
  • don't forget to reload apache after: `a2ensite CONFIGFILE.conf && apache2ctl configtest && systemctl restart apache2` – AK_ Feb 16 '21 at 19:06
1
<VirtualHost *:80>
    DocumentRoot "/var/www/example"
    ServerName *.example.org
    RedirectPermanent / http://example.com/
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/example/htdocs"
    ServerName example.org
    <Directory /var/www/example/htdocs>
         Options FollowSymLinks
         AllowOverride All
         Order Allow,Deny
         Allow from all
    </Directory>
</VirtualHost>

For the error 403 maybe you didn't set default document, so it try to access to folder content. For default document you can use for example

DirectoryIndex index.html Index.htm index.php
Froggiz
  • 3,043
  • 1
  • 19
  • 30
  • If I add the redirect to the main virtual host rule, it would try to also redirect the empty subdomain, wouldn't it? Doesn't virtual host rules only apply if their `ServerName` matches the request? – danijar Oct 06 '15 at 13:49
  • sorry i updated my answer, you are right, i should take more time to answer :P – Froggiz Oct 06 '15 at 14:00
  • Thanks. I tried to move the block above the config for the empty subdomain and added the `DocumentRoot` but that but that doesn't change the behavior. Also, you have both `example.org` and `example.com` in your answer. Not sure if that's just a typo. In my scenario, they're the same domains. – danijar Oct 06 '15 at 15:40
  • can you check your apache log in /var/log/apache/error.log & access.log (and other if you configured more), try to increase log level if you dont see anything – Froggiz Oct 06 '15 at 15:43