1

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]
WhiteFang
  • 199
  • 1
  • 10
  • I found an solution just add: # Rewrite all non-existing domains to example.com RewriteCond %{HTTP_HOST} !example\.com$ [NC]
    RewriteRule ^.*$ http://www.example.com%{REQUEST_URI} [R=301,L] In /httpdocs/.htacces (webroot folder)
    – WhiteFang Jan 12 '14 at 17:57

2 Answers2

1

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]
WhiteFang
  • 199
  • 1
  • 10
0

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.

Jon
  • 12,684
  • 4
  • 31
  • 44
  • Thanks for the temporary solution but this will only work if you are comming from the IP address. I like to have a solutions with catches all non-existing domains/directories. – WhiteFang Jan 07 '14 at 12:53
  • I found a solution for my question and added it as an anwser. – WhiteFang Jan 12 '14 at 18:03