64469

Redirect Users based on IP Address | Apache / htaccess

Question:

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

How do I do this?

Thank you.

Answer1:

The <a href="http://httpd.apache.org/docs/2.0/misc/rewriteguide.html" rel="nofollow">mod_rewrite</a> way:

RewriteEngine on RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.[8-9]$ # your ip here RewriteCond %{REQUEST_URI} !^/index/ RewriteRule .? /index/ [R,L]

Answer2:

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

Recommend

  • In React is it always better to render a Redirect than use this.props.history.push?
  • alternative of else operator and not equal operator in nginx
  • Showing my country based on my IP, mysql optimized
  • Missing Content-Length header when using Nginx + Gzip + Unicorn
  • Redirect site with .htaccess but exclude two+ folders
  • too many redirects for wordpress on nginx with apache2
  • Redirect only with specific domain
  • .htaccess mod_rewrite Unknown number of Variables of a GET form
  • secure way of passing form variable
  • Mod_rewrite: Include query string when redirecting
  • (Un)setting a cookie in mod_rewrite
  • Too many Redirects after switching WordPress to https
  • Accessing environment variables set using mod_rewrite in PHP
  • Htaccess Public Folder Rewrite Rule
  • Can I used mod_rewrite to change file extensions? .jpeg to .jpg for example
  • htaccess will not rewrite subdomains
  • .htaccess rewrite: subdomain as GET parameter and filepath afterdomain intact
  • Smart URL with optional parameters?
  • How to avoid using dynamic variables / a billion if statements in Java?
  • Google cloud datastore emulator init data
  • Help with mod_rewrite
  • Removing the .php file extension from the URL Request
  • Google OAuth2 for an web application hosted behind NAT (intranet server without public IP)
  • How to resolve docker host names (/etc/hosts) in containers
  • Should I be afraid to use UDP to make a client/server broadcast talk?
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • Meteor: Do Something On Email Verification Confirmation
  • Meteor helpers not available in Angular template
  • C++ Partial template specialization - design simplification
  • Cannot resolve symbol 'MyApi'
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Cassandra Data Model
  • htaccess rewriting URLs with multiple forward slashes
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Proper way to use connect-multiparty with express.js?
  • using HTMLImports.whenReady not working in chrome
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Conditional In-Line CSS for IE and Others?