Restrict all traffic except your IP address and more with .htaccess

    In some instances you might be looking to redevelop a website on a primary domain of say www.example.com. You’ll want your users to be redirected in the interim to a temporary site. You can do this easily with a .htaccess.

    If I am staying in one or two locations for the duration of the development, I usually just redirect all traffic except my IP address. However, if I’m a situation were I am moving around a lot or have a client who would like to see the progress of the development I create an alias or CNAME (depending on the situation) so that I can access the site via the alternative domain.

    Please note, this will not work if you are using a service such as CloudFlare or similar CDN network. Below is the simple piece of code that should be placed at the top of your .htaccess file.

    <IfModule mod_rewrite.c>
    	RewriteEngine on
    
        # If the IP address does not match this one
    	RewriteCond %{REMOTE_ADDR} !^110.110\.110\.110$
    
        # And they are not accessing a certain subdirectory
        RewriteCond %{REQUEST_URI} !^/admin
    
        # And they are not accessing the development domain
    	RewriteCond %{HTTP_HOST} !^development\.example\.com$
    
        # Redirect them to a temporary site
    	RewriteRule ^(.*)$ http://www.construction.com [R=302,L]
    </IfModule>

    You can add/remove additional RewriteCond’s as needed. If a condition is met, say the IP address matches, the evaluation of rules will stop and move onto any other rules you might have in your file (passwords, internal rewriting rules etc.).

    This post was last modified on January 24, 2014 11:35 pm

    Dom Sammut: Dom Sammut is a PHP / Node.js Web Developer from Australia with extensive experience in developing in and customising Laravel, Express, VueJS, WordPress, Symphony CMS, Craft CMS and Squiz Matrix.
    Related Post