0

Does anyone know is it possible to setup apache vhost in a following way:

  • if user access to www.example.com then vhost takes it to /www/example.com
  • if user access to www.example2.com then vhost takes it to /www/example2.com
  • if user access to an IP of server 1.2.3.4 then it gets some default page /www/default

Is it possible to make a generic configuration that will avoid having exact IP address of the server in it ? (so that if server IP changes it's not neccessary to change configuration files)

  • I recommend you to block all IP-based vhosts to avoid scanners. I block it with modsecurity rules from modsecurity_crs_21_protocol_anomalies.conf – GioMac Aug 14 '13 at 20:21
0

For your default page it would like this (this allows any IP address to go to the default page). The * is a wildcard for the IP address.

NameVirtualHost *:80

For virtual hosts your config file would look like this:

<VirtualHost *:80>
        DocumentRoot "/www/example1.com/"
        ServerName example1.com    
        ServerAlias www.example1.com
</VirtualHost>
<VirtualHost *:80>
        DocumentRoot "/www/example2.com/"
        ServerName example1.com    
        ServerAlias www.example1.com
</VirtualHost>
  • and where did you tell it to go to /www/default when an IP is entered as URL ? – Marko3d Aug 14 '13 at 18:41
0

if you re using seperate config files for the virtual hosts, for example in the subdirectory /etc/apache2/vhosts.d/, then you can just create one config with a name starting with 0_ and then any name (0_default_host.conf) and use just any ServerName. This config will be used for every request which is coming without proper hostname.

In this config file you can just create a redirect which is redirecting every request to nothing. Something like that: RewriteRule .* - [F]

This setup will block every hostname and ipaddress which is not handeled by the other config files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.