当前位置: 动力学知识库 > 问答 > 编程问答 >

Redirect everyone except my ip index.php

问题描述:

i am running a website and my website root directory have .htaccess and index.php, now i want to redirect all user's/traffic to "Under-Construction.php" page except my ip address.

i.e

www.example.com redirect to "www.example.com/under-construction/under-construction.php"

except my ip address.

  1. How can do this with .htaccess.
  2. How can do this with index.php.

    example: if $_SERVER['REMOTE_ADDR'] is 123.123.123 redirect index.php else redirect "www.example.com/under-construction/under-construction.php"

Regards.

网友答案:

Like this with php. Put the code at the top of index.php.

if (!in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '123.123.123.123')))
{
  header('Location: http://www.example.com/under-construction/under-construction.php');
  exit;
}
网友答案:

I tested this on my server:

    <?php
       // echo $_SERVER['REMOTE_ADDR'];
       // For proof of concept
        if($_SERVER['REMOTE_ADDR'] == "123.123.123.123") {
            header( 'Location: http://www.google.com/' ) ;
        }
    ?>
    

(Obviously that's not my ip ;)

Just as a side note, this may not be the best way to filter incoming clients, especially if you don't have a static ip address that you'll be requesting from. Perhaps set a cookie on your machine that tells the site who you are? Or add a $_GET parameter i.e. if $_GET['dev'] == true then redirect.

网友答案:

Use this .htaccess code

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/index.php$
RewriteCond %{REMOTE_ADDR} !^127.0.0.1$    # replace 127.0.0.1 with your IP address
RewriteRule ^ http://www.example.com/under-construction/under-construction.php
</IfModule>

Note: Replace 127.0.0.1 with your IP address

分享给朋友:
您可能感兴趣的文章:
随机阅读: