Want to redirect all visitors except for me
Asked Answered
T

5

20

Basically I'm about to start work on a site and I'd like something that I can add into my .htaccess file (or elsewhere) that'll work like this pseudo code: (my ip will be in place of 127.0.0.1)

if (visitors_ip <> 127.0.0.1)
    redirectmatch ^(.*)$ http://www.example.com/under-construction.html

Hopefully that makes sense...

Teishateixeira answered 16/11, 2008 at 0:26 Comment(0)
A
32

That would be something like:

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1

RewriteCond %{REQUEST_URI} !/mypage\.html$  

RewriteRule .* http://www.anothersite.com/mypage.html [R=302,L]

As Andrew points out, the %{REQUEST_URI} condition avoids infinite loop if you redirect to the same domain.

As Xorax comments almost 9 years later:

You should not use REMOTE_HOST, it will fail in many case. You should use REMOTE_ADDR.
Cf "difference between REMOTE_HOST and REMOTE_ADDR"

Acacia answered 16/11, 2008 at 0:36 Comment(3)
Now that IPv6 is in the picture, I'm finding there are cases where the localhost IP address comes through as ::1, not always 127.0.0.1 -- not exactly sure when and why, but to handle that I think you need an additional condition: RewriteCond %{REMOTE_HOST} !^::1 The colons should not require escapes -- not special characters for the rewrite engine regex, I don't believe.Gearalt
You should not use REMOTE_HOST, it wiill fail in many case. You should use REMOTE_ADDR. #3812666Fellowman
@Fellowman Thank. I have amended my 9-years old answer.Acacia
T
3

Here's the solution I ended up using, note that it is similar to VonC's except that his caused an infinite loop if you chose to redirect to the same domain.

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^127\.0\.0\.1
RewriteCond %{REQUEST_URI} !/coming-soon\.html$ 
RewriteRule .* http://www.andrewgjohnson.com/coming-soon.html [R=302,L]

It should also be noted that 302 is a temporary move and should be used instead of nothing or 301.

Teishateixeira answered 17/11, 2008 at 19:49 Comment(1)
I just added your extra rewrite condition in my answer, to be thorough .Acacia
B
2
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect all except allowed IP
ReWriteCond %{REMOTE_ADDR} !^000\.000\.000\.001$
RewriteCond %{REMOTE_ADDR} !000\.000\.000\.002$
ReWriteCond %{REMOTE_ADDR} !^000\.000\.000\.003$
RewriteRule (.*) http://YourOtherWebsite.com/$1 [R=301,L] 
</IfModule>

before your wordpress ifmodule this will redirect everyone except the 3 ip address in question.

You simply ftp to the site and edit the .htaccess file if your IP address changes.

Bloat answered 13/4, 2013 at 21:37 Comment(0)
J
0

Be careful with this approach.

I've gotten burned by taking the IP based approach to limiting access, and then losing the lease on my IP address.

Of course you can always ssh into the box in question and change the .htaccess file again, but the 5 minutes of panic while you try to figure out what just happened aren't exactly fun if you aren't expecting it to happen.

I recommend instead using the .htaccess (in conjunction with an htpasswd file) to request credentials for accessing your development site.

A good example of that is here: http://aplawrence.com/foo-web/htaccess-authentication.html

Japonica answered 16/11, 2008 at 0:47 Comment(2)
Ya thanks, good point but I won't be keeping this line up for more than a week or two.Teishateixeira
Depending on how your network is set up, your lease times could be much shorter than that.Japonica
B
0
<IfModule mod_rewrite.c>
 RewriteEngine On
#IPv4
 RewriteCond %{REMOTE_ADDR} !^26\.122\.33\.214 
#IPv6
 RewriteCond %{REMOTE_ADDR} !^2a01\:3f0d\:e31e\:c410\:98b\:a6ff\:45b0\:5132
#avoid infinite loop
 RewriteCond %{REQUEST_URI} !/offline/index.html$ [NC]
#allow images to be accessed
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
#redirect to the offline page or other URL.
 RewriteRule .* /offline/index.html [R=302,L]
</IfModule>
Basinet answered 7/3, 2023 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.