Welcome to WebmasterWorld Guest from 54.159.246.164

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Issue with Name-Based Virtual Hosts

The first <VirtualHost> entry gets pulled up even if it's not a match

   
8:57 am on Jan 9, 2006 (gmt 0)

10+ Year Member



I've been using a name-based virtual host setup for a while.

Something I noticed recently is that if I point DNS for a random domain to the IP address that has multiple <VirtualHost> entries for it, the first (or "default") <VirtualHost> will be used and the website for it displayed, but in the address bar the random domain name is displayed.

I can just imagine someone pointing an explicit domain name to my website and having the website displayed under that domain name. I use relative links, so someone could navigate the website from any domain name.

Is there any easy way to block this from happening?

If not (which I would imagine is likely the case) is there a simple way to include a mod_rewrite to redirect the domain so while a random domain name could still point to the website, the website wouldn't be displayed under it?

I've tried to do this, but haven't been successful because it redirects the other websites hosted, not just if there isn't any match.

Thanks.

9:02 am on Jan 9, 2006 (gmt 0)

10+ Year Member



The only solution I can come up with is to have one rewrite condition that checks the requested domain against a list of all the domains hosted and if it doesn't match any of them, then it redirects the user to the first/default <VirtualHost> domain in httpd.conf.
11:43 am on Jan 9, 2006 (gmt 0)

10+ Year Member



The first virtualhost after each NameVirtualHost entry is the catchall entry, so if you don't want to return a site for non-existant domain names, create an empty site (so it returns a 404 for all page requests) as the first virtualhost after each NameVirtualHost entry.

You can even customize the 404 page so it says "host not found" for example.

6:50 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



"The only solution I can come up with is to have one rewrite condition that checks the requested domain against a list of all the domains hosted and if it doesn't match any of them, then it redirects the user to the first/default <VirtualHost> domain in httpd.conf."

How exactly do you do this? Specifically how do create the list and use it in a rewrite condition?

"The first virtualhost after each NameVirtualHost entry is the catchall entry, so if you don't want to return a site for non-existant domain names, create an empty site (so it returns a 404 for all page requests) as the first virtualhost after each NameVirtualHost entry.

You can even customize the 404 page so it says "host not found" for example."

By empty, do you mean like this?

<VirtualHost *:80>
ServerAdmin
DocumentRoot
ServerName
ServerAlias

RewriteEngine on
RewriteRule ^/$ /location/of/404/file.html [R=404,L]
</VirtualHost>

Or this?

<VirtualHost *:80>
RewriteEngine on
RewriteRule ^/$ /location/of/404/file.html [R=404,L]
</VirtualHost>

Thanks!

10:49 am on Jan 10, 2006 (gmt 0)

10+ Year Member



By Empty I mean - have the document root point to a directory with no files in it, or just the 404 page in it.

NameVirtualHost *:80 

<VirtualHost *:80>
ServerName default-80
DocumentRoot "/www/default-80"
DirectoryIndex default.htm
ErrorDocument 404 /404.html
</VirtualHost>

10:26 pm on Jan 10, 2006 (gmt 0)

10+ Year Member



Thanks bob.

I just realized that I responded to myself.

Wow, I needed to get some sleep.