Redirect Users based on IP Address | Apache / htac

2019-08-18 04:57发布

I'd like to redirect users to an /index/ area of the site if they don't have my IP address.

How do I do this?

Thank you.

2条回答
放我归山
2楼-- · 2019-08-18 05:17

Is this what you're looking for?

if($_SERVER['REMOTE_ADDR'] != 'xxx.xxx.xxx.xxx')
{
    header('Location: /index/');
}

You can specify an array that matches against allowed IPs.

if(!in_array($_SERVER['REMOTE_ADDR'], array('xxx.xxx.xxx.xxx', 'xxx.xxx.xxx.xxx')))
{
    header('Location: /index/');
}
查看更多
爷的心禁止访问
3楼-- · 2019-08-18 05:31

The mod_rewrite way:

RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.[8-9]$ # your ip here
RewriteCond %{REQUEST_URI} !^/index/
RewriteRule .? /index/ [R,L]
查看更多
登录 后发表回答