Apache VirtualDocumentRoot redirect non-existing domains

梦想的初衷 提交于 2019-12-23 03:54:49

问题


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]

回答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]



回答2:


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.



来源:https://stackoverflow.com/questions/20971407/apache-virtualdocumentroot-redirect-non-existing-domains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!