homepage Welcome to WebmasterWorld Guest from 54.226.80.55
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Issue with Name-Based Virtual Hosts
The first <VirtualHost> entry gets pulled up even if it's not a match
moheybee




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

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.

 

moheybee




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

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.

uncle_bob




msg:1523888
 11:43 am on Jan 9, 2006 (gmt 0)

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.

moheybee




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

"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!

uncle_bob




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

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>

moheybee




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

Thanks bob.

I just realized that I responded to myself.

Wow, I needed to get some sleep.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved