Redirect Users based on IP Address | Apache / htaccess

一世执手 提交于 2019-12-12 03:03:10

问题


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.


回答1:


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]



回答2:


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/');
}


来源:https://stackoverflow.com/questions/9911289/redirect-users-based-on-ip-address-apache-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!