Mod Rewrite - directing HTTP/HTTPS traffic to the appropriate virtual hosts

Posted by kce on Pro Webmasters See other posts from Pro Webmasters or by kce
Published on 2011-02-19T00:46:28Z Indexed on 2011/02/21 23:34 UTC
Read the original article Hit count: 430

Filed under:
|
|
|

I have an Apache2 web server (v. 2.2.16) running on Debian hosting three virtual hosts. The first two hosts are HTTP only (server1 and server2). The last host is HTTPS only (server3). My virtual host configuration files can be found at pastebin. I would like to use mod rewrite to get the following behavior:

Currently, requesting http://server3 gives you a 403 because indexing is disabled for that host and a request for https://server1 or https://server2 will resolve as https://server3 (as its the only virtual host running SSL). This behavior is not desirable.

So far I have added a rewrite rule to the central configuration file (myServerWideConfs.conf), with unfortunately no effect. I was under the impression that this rule (or something similar) should rewrite all https:// requests for server1 and server2 to the proper http:// request.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^server3 [NC]
RewriteRule (.*) http://%{HTTP_HOST}

My question is two-fold:

  • What mod rewrite rules should I use to accomplish this? And where should they go? Debian's packaging of Apache has a pretty granular (i.e., fractured) configuration file layout; should my rewrite rules go in /etc/apache2/apache2.conf, /etc/apache2/conf.d/myServerWideConfs.conf, or the individual virtual host files?
  • Is mod rewrite the right tool to accomplish this or am I missing something in my greater apache configuration?

© Pro Webmasters or respective owner

Related posts about apache

Related posts about mod-rewrite