Easy 301 redirect to www

Jan 10 2012 Published by under Info, News

There are quite a few ways to automatically redirect your site visitors from a non-www to a www URL on your website.  In the past, I’ve always setup a ServerAlias in apache and these ReWrite rules to accomplish this.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.somedomain.com$
RewriteRule ^/(.*) http://www.somedomain.com/$1 [R=301]

Recently, I’ve run into this more and more and prefer an easier solution.

Case in point: you are using Amazon ELB to host your site on a few backend webserver instances.  Amazon gives you a hostname which is the public address of your cluster of webservers (something like yourloadbalancer-1234567890.us-east-1.elb.amazonaws.com)  The problem is you can’t set your root domain to use a CNAME.

Solution:  Use Millcreek Systems’ free www redirect service.

  • In DNS, set your root domain to point to 75.101.129.58
  • In DNS, set www to point to the real location of your site, the CNAME of your ELB or even an A record for your server(s) IP address(es).
  • Sit back and enjoy.

No responses yet

Quick programming tip for servers behind load balancers

Oct 28 2011 Published by under Info

If you’re using PHP and wanting to check to make sure the incoming connections came over HTTPS, you are probably using the $_SERVER['HTTPS'] variable.

The problem is, if your servers are behind a load balancer which handles SSL encryption for you, this method of checking won’t work.  Fortunately, there are other headers added by the load balancer you can use to detect SSL.  They are the X-Forwarded-* headers.

For example:
$headers["X-Forwarded-For"] == 123.45.67.89 (because $_SERVER['REMOTE_ADDR'] is going to give you the load balancer’s IP address)
$headers["X-Forwarded-Port"] == 443
$headers["X-Forwarded-Proto"] == https

These headers should work with all loadbalancers, including Amazon’s ELB on EC2.

 

No responses yet