Htaccess: Redirect all users except your IP

Follow @tristarweb

I was doing a web design project earlier that required me to work on the live domain. I didn’t want any visitors to see what I was doing, so I set up a temporary blog at temp.mydomain.com. All I needed to do then was to redirect all visitors to that domain, but allow me to stay on the main domain of www.mydomain.com, and continue my maintenance.

The solution was simple; I simply created a .htaccess file on my main domain, in the root folder. The htaccess contained the following information:

RewriteEngine on
RewriteCond $1 !^http://www.mydomain.com/temp
RewriteCond %{REMOTE_HOST} !^12\.345\.678\.901
RewriteRule (.*) http://temp.mydomain.com/$1 [R=301,L]

It really is that simple to redirect all visitors, except you own IP, to a subdomain. The script above does the following:

  • Turn on the rewrite engine (no need to do this if it is already on in your htaccess).
  • Exclude the folder /temp from my rewrite (this is where the files for my subdomain are stored).
  • Exclude the IP address of 12.345.678.901 from my rewrite rule (you can find out your IP address by going to whatismyip).
  • I am then telling it to redirect everything (.*) to my subdomain http://temp.mydomain.com.

All you need to do is swap in the addresses for you subdomain files, your IP address, and your subdomain address. Simple as that!

About James

Hello there, I'm James. I studied Multimedia Design at university. I'm a designer at heart, though I love developing innovative and exciting solutions to web based products.I now specialise in front end web design and bespoke Wordpress powered websites.

Be Social:

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

One Response to Htaccess: Redirect all users except your IP

  1. Kittie says:

    06:18 30/04/2011

    Yes,even i too faced same problem and resolved with htaccess file.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please wrap all source code with [code][/code] tags.

Follow Tristar on Twitter