Redirect 301 for everyone but you

Consider this post just a quick note to myself, because I will surely need this again in the future.

Scenario: i have a production server and a development server. As you can easily guess, on the production server i have the live version of the websites and on the development server I develop new features/bug fixes/new websites.

What I usually do is to create a specific subdomain for the development. Sometimes i add that hostname as DNS record, sometimes i just add it to my hosts file. Either way, it’s still possible for others to find your “hidden” development website (for example, Alexa.com displays the subdomains getting traffic, so if you use the Alexa toolbar – not necessarily the official – you will see your development website listed under your domain statistics on Alexa.com).

The solution to the problem is extremely simple: just redirect to the production website the traffic coming from any IP address but yours. Just add to the .htaccess of the development website these lines:

RewriteEngine On
# If it’s not your IP address
RewriteCond %{REMOTE_ADDR} !^1.2.3.4$
# Redirect everything to production host.
RewriteRule ^(.*)$ http://www.stefanogorgoni.com/$1 [R=301,L]

Change of course that 1.2.3.4 to your IP address (if you have a dynamic IP, don’t forget to change the .htaccess accordingly every time the IP changes), and the hostname of destination (the www.stefanogorgoni.com part).