A quick and dirty proxy that (nearly) anyone can do

by Rob Haswell
Bronco - Digital Marketing Agency

So we were looking for some hosting in the states for one of our sites, and we were frustrated that all the AdWords on Google.com were geotargeted to our UK IP address. So, I came up with dirty little hack to get the pages we wanted via one of our US servers, without the need to mess around with my browser settings.

You need:

  1. A domain that you can add wildcard DNS entries to
  2. An Apache hosting account that will accept a wildcard ServerAlias in the VHost directive. Or IP-based hosting would do it.
  3. .htaccess support
  4. The Apache modules mod_proxy and our old favourite, mod_rewrite

Not too much to ask, I’d say. You can definitely get this on any Managed hosting deal, whether you have root or not.

All that you need to do is:

  1. Pick a domain to use. For this example we’ll be using “example.com”, and so as not to mush up an entire domain, we’ll use the sub “proxy.example.com”
  2. Add an appropriate DNS entry such that *.proxy.example.com points to your server
  3. Set up your server so that one of your VHosts picks up *.proxy.example.com
  4. Stick this in your .htaccess and smoke it:
    [code]RewriteEngine On
    RewriteBase /
    RewriteRule ^_magic_/(.*).proxy.example.com/(.*)$ http://$1/$2 [P]
    RewriteRule ^(.*)$ /_magic_/%{HTTP_HOST}/$1[/code]
    Note: Don’t forget to put your domain in to line 3

Now all you need to do is stick “.proxy.example.com” on the end of the domain bit of any URL you want to visit, eg:

  • http://www.google.com.proxy.example.com/search?hl=en&q=dedicated+servers
  • http://httpd.apache.org.proxy.example.com/docs/2.0/mod/mod_rewrite.html#rewriterule

This works a bit like the Coral CDN, where you place “.nyud.net:8080” on the end of your host.

Warning: This is vulnerable to HTTP redirection (301, 302) – eg if you visit http://google.com.proxy.example.com/ you are redirected to http://www.google.com/ by Google’s host canonicalisation.

Anyone wondering what that script does? Well I’ll walk you through it line-by-line:

1, 2: Set up mod_rewrite
Line 4: This is where the rewriting starts (because the first one is skipped initially). This line turns something like “http://foo.com.proxy.example.com/bar.html” into “http://foo.com.proxy.example.com/_magic_/foo.com.proxy.example.com/bar.html“. We do this because we want to rewrite on the Host: header, which mod_rewrite isn’t designed to do. To enable us to do this we artificially place the Host: header, represented as %{HTTP_HOST}, into the URL-path.
Line 3: This line matches the changes made by line 4, to allow us to match out what we want from ${HTTP_HOST} and construct a URL to redirect to. The [P] flag causes this line to by carried out by the proxy module.

Easy, yeah? I don’t know why you didn’t think of it before.


Bronco - Digital Marketing Agency
Making your inbox more interesting
Looking to keep up to date, or find out those things we can’t mention on the blog? Then sign up to our semi-regular newsletter. Don’t worry, we won’t spam you.


Get in Touch

Things are better when they’re made simpler. That’s why the David Naylor blog is now just that; a blog. No sales pages, no contact form - just interesting* info about SEO.

If you’d like to find out more about the Digital Marketing services we do provide then head over to Bronco (our main company website) to get in touch.

Get in Touch Today * Interestingness not guaranteed
Part of the Bronco family