Redirect all subdomains to main domain inside vhost
Solution 1
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>
Solution 2
<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
Related videos on Youtube
danijar
Researcher aiming to build intelligent machines based on concepts of the human brain. Website · Twitter · Scholar · Github
Updated on September 18, 2022Comments
-
danijar over 1 year
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.-
Froggiz over 8 yearsAny news about your question ?
-
danijar over 8 yearsNope, unfortunately not.
-
Froggiz over 8 yearscan 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
-
-
danijar over 8 yearsIf 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? -
Froggiz over 8 yearssorry i updated my answer, you are right, i should take more time to answer :P
-
danijar over 8 yearsThanks. 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 bothexample.org
andexample.com
in your answer. Not sure if that's just a typo. In my scenario, they're the same domains. -
Froggiz over 8 yearscan 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
-
AK_ over 3 yearsdon't forget to reload apache after:
a2ensite CONFIGFILE.conf && apache2ctl configtest && systemctl restart apache2