Dynamic Virtual Hosting with Multiple Domain Names
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Dynamic Virtual Hosting with Multiple Domain Names
Using a RHEL 5.3 server, I am trying to setup dynamic virtual hosting where sub.companyname.com goes to /var/www/internal/sub and clientname.previewserver.com goes to /var/www/clients/clientname/dev
however when I go to http://scripts.companyname.com it still looks in /var/www/clients/scripts/dev instead of /var/www/internal/scripts
I'm rather new with dynamic virtual hosting and I was lucky in getting the first implementation working, but obviously I need more information on the subject. Everywhere I searched, it was pushing for rewrite cond/rules, which I think is more complex than for my simple needs.
Yes, I did. See the reason why my first attempt did not work is that the separate dynamic virtual hosting configurations require separate IP bindings. It would have worked, if I had already migrated our other IPs to this new server since companyname.com is bound to its own IP on the same machine. However, I wanted to get everything working prior to migrating additional addresses, and I succeeded, but I had to use the RewriteEngine.
In case anyone is eager to see the solution, here is my one an only VirtualHost
Code:
<VirtualHost *:80>
LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon
RewriteEngine On
# The first rewrite is to enforce the usage of www by throwing a 301 to non-www addresses
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
# The next rewrite is to search for people accessing our internal subdomains
RewriteMap lowercase int:tolower
RewriteCond ${lowercase:%{SERVER_NAME}} ^www\.[a-z-]+\.companyname\.com
RewriteRule ^(.*) ${lowercase:%{SERVER_NAME}}$1 [C]
RewriteRule ^www\.([a-z-]+)\.companyname\.com/(.*) /var/www/internal/$1/$2
# The next rewrite is use a map file to point the extra domain names to their document roots
RewriteMap vhost txt:/var/www/vhost.map
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ /var/www%1/$1
# Then finally, the preview server sites
VirtualDocumentRoot /var/www/clients/%-3/dev
ScriptAlias /cgi-bin/ /var/www/std-cgi
</VirtualHost>
I know that I should probably turn the last bit into a rewrite as well, as I know that if some clientname.otherdomainpointedatus.com will pull up the preview sites, but I'm not too worried about that, since these are the folders with robots.txt files to stop the search spidering. Also the vhost.map file contains...
I'm sure someone could probably propose some revisions to this, but at least it is working and I can start slowly migrating our sites to this new server and I don't have the reload httpd to add new vhosts now.
Last edited by bahbahthelamb; 11-18-2009 at 04:57 PM.
<VirtualHost *:80>
LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon
RewriteEngine On
RewriteMap lowercase int:tolower
# First to always add the WWW
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
# Here is all the redirects for Domain Forwards, using a forwarder map
RewriteMap forwarder txt:/var/www/forwarder.map
RewriteCond ${lowercase:%{HTTP_HOST}} ^(.+)$
RewriteCond ${forwarder:%1} ^(/.*)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Now the rewrites for the internal subdomains
RewriteCond ${lowercase:%{SERVER_NAME}} ^www\.[a-z-]+\.companyname\.com
RewriteRule ^(.*) ${lowercase:%{SERVER_NAME}}$1 [C]
RewriteRule ^www\.([a-z-]+)\.companyname\.com/(.*) /var/www/internal/$1/$2
# Now the rewrites for the preview sites
RewriteCond ${lowercase:%{SERVER_NAME}} ^www\.[a-z-]+\.previewsite\.com
RewriteRule ^(.*) ${lowercase:%{SERVER_NAME}}$1 [C]
RewriteRule ^www\.([a-z-]+)\.previewsite\.com/(.*) /var/www/clients/$1/dev/$2
# Now the rewrites for other domains, using a vhost map
RewriteMap vhost txt:/var/www/vhost.map
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ /var/www%1/$1
</VirtualHost>
This now works with 301 redirects based on a forwarders' map, but there is one issue, for some reason it won't forward unless it is just the root of the forwarded domain name. For now, I just have the forwarded domains still listed in the vhost.map file so that there's no 404 pages, but I have to figure this out. I've tried checking off of %{HTTP_HOST} as well as %{SERVER_NAME}.
Got this all working, I'm calling it RC1, since it is functional, but there is definitely more improvements that I can think of.
Code:
<VirtualHost *:80>
# Set Logs
LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon
# Initialize mod_rewrite
RewriteEngine On
RewriteMap lowercase int:tolower
# First to always add the WWW
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
# Here is all the redirects for Domain Forwards, using a forwarder map
RewriteMap forwarder txt:/var/www/forwarder.map
RewriteCond ${lowercase:%{HTTP_HOST}} ^(.+)$
RewriteCond ${forwarder:%1} ^www\.(.*)$
RewriteRule ^(.*)$ http://www\.%1$1 [R=301,L]
# ------- /var/www/forwarder.map ------- #
# www.old-domain.com www.new-domain.com #
# -------------------------------------- #
# Now the rewrites for the internal subdomains
RewriteCond %{REQUEST_URI} !^/error/ # Make sure global error folder is accessible
RewriteCond ${lowercase:%{SERVER_NAME}} ^www\.[a-z-]+\.companyname\.com
RewriteRule ^(.*) ${lowercase:%{SERVER_NAME}}$1 [C]
RewriteRule ^www\.([a-z-]+)\.companyname\.com/(.*) /var/www/internal/$1/$2
# Now the rewrites for the preview sites
RewriteCond %{REQUEST_URI} !^/error/ # Make sure global error folder is accessible
RewriteCond ${lowercase:%{SERVER_NAME}} ^www\.[a-z-]+\.aoipreview\.com
RewriteRule ^(.*) ${lowercase:%{SERVER_NAME}}$1 [C]
RewriteRule ^www\.([a-z-]+)\.aoipreview\.com/(.*) /var/www/clients/$1/dev/$2
# Now the rewrites for other domains, using a vhost map
RewriteMap vhost txt:/var/www/vhost.map
RewriteCond %{REQUEST_URI} !^/error/ # Make sure global error folder is accessible
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ /var/www%1/$1
# --------------- /var/www/vhost.map --------------- #
# www.client-domain.com clients/clientname/livesite #
# www.internal-domain.com internal/sitename/ #
# -------------------------------------------------- #
# Default root, in case nothing is found above
VirtualDocumentRoot /var/www/internal/
</VirtualHost>
I was also thinking of making RewriteCond's to manage my robots.txt files since they are currently managed per server root (which is obviously a great quantity); that'll be v1.1
Last edited by bahbahthelamb; 11-19-2009 at 03:01 PM.
There is a serious issue that has risen from using this, the .htaccess files are not working now. I already have a few live sites on this new server, and now I have gotten to one that uses a custom 404 page. I was thinking no big deal, but after trying for a while I realized that it is not processing the .htaccess files at all. I checked to ensure that httpd.conf had
Code:
AccessFileName .htaccess
For testing I temporarily removed this from httpd.conf
Code:
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>
This was mostly to ensure that I didn't have some global location setting for the access file that I didn't know about. After removing this, and then sending a server request for /.htaccess, it does pull the local access file. I added this code to get it working and buy more time:
However, ultimately I would like to get Access Files working so I can manage site overloads in a decentralized manner without the need for restarting Apache with every minor changes. For this specific situation, I wouldn't mind a centralized, look for a local 404.php file first and then go to the global. I've been looking through the forums all morning trying to seek related cases, but it looks like there are too few people dealing with complex dynamic virtual hosting. Chances are I am going to find my own answer (and post it here for others), but if someone stumbles upon this and has some input, it is greatly appreciated.
Last edited by bahbahthelamb; 02-10-2010 at 12:58 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.