Chad Warren
Chad Warren

Reputation: 107

Only Allow requests from specific IP address to access subdomain & redirect all others to a different url - htaccess

I have a subdomain for our old store of store.mydomain.com . We just launched a new store at the root domain www.mydomain.com

How can we redirect all traffic from store.mydomain.com to www.mydomain.com for all traffic EXCEPT for those from a specific IP?

In other words we are trying to maintain internal access to store.mydomain.com because we have some things we still need to do with the old store (customer records, orders, etc...) but we don't the general public to be able to access any URI on the old store.mydomain.com subdomain. We want all of those people to be redirected to www.mydomain.com

The following works to redirect ALL traffic, but unsure of how to do the IP condition check.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^store.example.com$
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]

Upvotes: 2

Views: 2383

Answers (1)

Jon Lin
Jon Lin

Reputation: 143846

Try:

RewriteEngine on 
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4$
RewriteCond %{REMOTE_ADDR} !^12\.34\.56\.78%
# etc...
RewriteCond %{HTTP_HOST} ^store.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]

So this will redirect for everyone except when the request comes from the IP:

1.2.3.4
12.34.56.78

Upvotes: 2

Related Questions