Question

I'am using VirtualDocumentRoot to have multiple domains and subdomains. Everything goes well until someone tries to reach my server by IP or an non-existing domain. For example 10.10.10.10 wich results into an 404 Not Found error. Maybe someone out there can help me find an solution to redirect non-existing domains or IP to my main domain www.example.com?

Let me be clear: non-existing subdomains are being redirected but non-existing domains are not.

HTTPD.CONF:

<VirtualHost *:80>
        ServerName default
        ServerAlias *
        #www.example.com == /httpdocs/example.com/www/
        VirtualDocumentRoot /httpdocs/%-2.0.%-1/%-3
    </VirtualHost>

/httpdocs/example.com/.htacces:

# Rewrite all non-existing subdomains to www.
RewriteCond %{HTTP_HOST} example\.com$
RewriteRule ^.*$ http://www.example.com%{REQUEST_URI} [R=301,L]
Was it helpful?

Solution

The Solution to my problem was:
To add this to .htacces in my webroot folder /httpdocs/.htaccess

This will cause all non-existing domains to end op in root folder where they are picked up by the .htaccess. (no symbolic links anymore)

# Enable Rewrite Engine
RewriteEngine on

# Rewrite all non-existing domains to example.com
RewriteCond %{HTTP_HOST} !example\.com$ [NC] #just add your main domain here
RewriteRule ^.*$ http://www.example.com%{REQUEST_URI} [R=301,L]

OTHER TIPS

There appears to be a "bug" in mod_vhost_alias whereby you can't specify a failover/default directory if the interpolated directory doesn't exist (see comments at the bottom of this page) so I don't think you can handle arbitrary domain names, you'll need to create directories for each of the domain names you want to match using your interpolation rules.

However, for the IP address, I believe if you create a symlink from this directory /httpdocs/230.239/132/ to /httpdocs/gdwebs.nl/www/ then that'll work.

Alternatively, if you can't create a symlink, create the /httpdocs/230.239/132/ directory and use htaccess to redirect requests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top