Search code examples
.htaccess

htaccess, Redirect all others except your domain


I have the following Htaccess file that redirects all other traffic except my ip.

How can I edit this to use a domain like dyndns instead since my ISP changes my IP?

Working Code

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^111.222.333.444
RewriteRule .* http://www.anothersite.com [R=302,L]

Non-working Code

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^myDomain.dyndns.com
RewriteRule .* http://www.anothersite.com [R=302,L]

**Edit: It appears that with my webhost this is impossible (at the time of posting) however, This can be accomplished with some simple php code that I will include below.


Solution

  • My End soultion was to insert the following code into my index.php page

     <?php
        $ip = gethostbyname('myDomain.DynDns.com');
        if( $ip == $_SERVER['REMOTE_ADDR'] ) 
            header("Location: http://example.com/ifLocalIpAddressSite");
        else 
        header("Location: http://example.com/IfExternalIpAddressSite");
        ?>