Mobile app version of vmapp.org
Login or Join
Mendez628

: Configure Apache virtual hosts - all subdomains to 404 It's the first time that I try to configure apache so I hope you can help me. This is what I want to achieve: All undefined subdomains

@Mendez628

Posted in: #Apache #Apache2 #Virtualhost

It's the first time that I try to configure apache so I hope you can help me.

This is what I want to achieve:


All undefined subdomains should lead to not found if they aren't defined
Root folder should never be accessible


Now this is what I have so far:

my.conf

ServerName server1 (is hostname)

ServerSignature off
ServerTokens prod

Options -Includes
Options -ExecCGI


sites-available/default

<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /htdocs

<Directory />
Options None
Order deny,allow
Deny from all
</Directory>

<Directory /htdocs/>
Options -Indexes -FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

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


sites-available/example

<VirtualHost *:80>
ServerAdmin webmaster@localhost

ServerName example.com
ServerAlias example.com
DocumentRoot /htdocs/example

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


Well, my questions are:


Does the "Directory /" from the default-config also apply on the example.com site? Or do I need to put it in there, too. Or should it rather be in the my.conf?
Same question for "Directory /htdocs/" - should I put this in the example.com-config, too? Of course like "Directory /htdocs/example/" then.
At the moment every subdomain of the example.com is leading to the default directory. How can I prevent that? Only the Server IP should lead there - if at all.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Mendez628

1 Comments

Sorted by latest first Latest Oldest Best

 

@Sims2060225

For undefined subdomains
you could add a new virtualhost AFTER all known subdomain.example.com virtualhosts.

<VirtualHost *:80>
ServerName *.example.com
Redirect 404 /
</VirtualHost>


Redirect 404 / replies with "404 Not found" on every request.



The scope of configuration directives

Directives can be server wide or restricted to particular directories, files, virtualhosts etc. Directory, DirectoryMatch, Files, FilesMatch, If, IfDefine, IfModule, IfVersion, Location, LocationMatch, Proxy, ProxyMatch and VirtualHost are called Configuration Section Containers: directives within them only affects inside the container.

In the following example "Require all denied" affects on /htdocs/private.html and "Options -Indexes" affects to "/htdocs" but only within VirtualHost with ServerName example.com.

<VirtualHost *:80>
ServerName example.com
<Directory /htdocs/>
Options -Indexes
<Files private.html>
Require all denied
</Files>
</Directory>
<VirtualHost *:80>

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme