Page 1 of 1

htaccess How To Allow Both HTTP and HTTPS Even with Proxies?

Posted: Sun Jun 28, 2009 8:36 am
by volomike
Let's say you've done the ground work on a couple of your hosting plans such that you can access your domains via http or https.

Now let's say you introduce Proxy Throughput (the [P] operator) so that certain subdomains can get routed to other servers, like so:

RewriteCond %{HTTP_HOST} =ricks-motorcycles.mysite.com
RewriteRule ^(.*)$ http://alpha2.com/ricks-motorcycles/$1 [P,L]

Where, if you have alpha1.com and alpha2.com, and mysite.com is parked at alpha1.com and this entry is in the .htaccess file, the guys at Rick's Motorcycles can access their site off of alpha2.com as if they were on mysite.com.

Okay, fine, but now how to get the https working and with the least amount of htaccess lines possible? This is important as far as line count because let's say I may have 500 or more entries like this in that file for users who sign up on my website.

Re: htaccess How To Allow Both HTTP and HTTPS Even with Proxies?

Posted: Sun Jun 28, 2009 8:59 am
by volomike
I found a solution, and I also found trouble with the previous way I was doing this.

I mean, let's say you want to have like 5000 customers sign up on your site, each getting subdomains, and each would be routed to various other shared servers in the background via this .htaccess technique. Little problem there. Eventually this file gets super huge and for those unlucky souls near the bottom of the list, they end up with every click being slow because this monstrous .htaccess file has to process requests. Sure, the [L] operator speeds this up, but when there's no match on subdomain, it continues going down the list until it finds a match.

The better solution is to switch from subdomains to subdirs. I see that Facebook.com, Twitter.com, and Basecamphq.com use this technique, for instance. So, instead of...

http://ricks-motorcycles.alpha.com/

...you would have...

http://alpha.com/ricks-motorcycles/

So now on alpha.com, you create subdirectories for each new company who signs up on your site. Now, because you might not have the disk space or processing capacity for all those companies on the same shared, VPS, or dedicated hosting plan, you drop a file like the following into those directories where you want to route them in the background to another server, and even account for https as well.

Code: Select all

# DON'T LET USERS SEE DIRECTORIES
Options -Indexes
 
# TURN OUR REWRITE ENGINE ON.
RewriteEngine On
 
# If https, route via https
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ https://alpha2.com/ricks-motorcycles/$1 [P,L]
 
# Otherwise, route via http
RewriteRule ^(.*)$ http://alpha2.com/ricks-motorcycles/$1 [P,L]
 
Now this thing can scale fairly fast because you don't have a long .htaccess file that gets parsed -- just a short one. As well, you handle http and https.

But note that this isn't the same as Load Balancing -- this is Proxy Throughput. Load Balancing in the Apache sense is where you handle the load for a given URL on one of multiple random servers. Proxy Throughput, on the other hand, is a one:one kind of thing where you handle the load for a given URL on only one assigned server. But the cool thing is that on the assigned server, you can implement Load Balancing even after the Proxy Throughput call, giving you the best of both worlds -- company isolation + load balancing. So, if you want to learn more about how to use Apache to do easy load balancing, just look at this doc under the section heading "Load Balancing":

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html