Forum Moderators: phranque

Message Too Old, No Replies

adding a temporary redirect to wordpress .htaccess

but excluding multiple client IP addresses

         

jtotheh

3:07 am on Jan 23, 2010 (gmt 0)

10+ Year Member



Hello .htaccess gurus! I always end up on this forum when searching for .htaccess solutions, so hopefully someone will know what i'm doing wrong here. I'm trying to add a temporary redirect for a splash page to an existing WordPress .htaccess file so that myself and other contributors can begin to add content to a new site without the public seeing. With the code below, even those at the excluded IPs are shown the splash page. Here's my code:


# Temporarily (302) redirect all traffic
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123 [OR]
RewriteCond %{REMOTE_ADDR} !^456\.456\.456\.456 [OR]
RewriteCond %{REMOTE_ADDR} !^789\.789\.789\.789
RewriteCond %{REQUEST_URI} !/splash\.html$
RewriteCond %{REQUEST_URI} !/splash\.jpg$
RewriteRule $ /splash\.html [R=302,L]
# End Temporary (302) redirect


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Thanks!

jdMorgan

4:30 am on Jan 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, because if an IP address *is* equal to address #1, then it is not going to be equal to address #2, therefore the second RewriteCond will match and the request will get redirected to the splash page. So the problem is that you should not be [OR]ing those RewriteConds.

RewriteEngine on
#
# Temporarily (302) redirect all traffic to splash page
RewriteCond %{REMOTE_ADDR} !=123.123.123.123
RewriteCond %{REMOTE_ADDR} !=456.456.456.456
RewriteCond %{REMOTE_ADDR} !=789.789.789.789
RewriteCond %{REQUEST_URI} !/splash\.html$
RewriteCond %{REQUEST_URI} !/splash\.jpg$
RewriteRule ^ http://www.example.com/splash\.html [R=302,L]

Note that the exact-string-match syntax should be faster. You could also use this syntax on the excluded 'splash' URLs if they are at the Web root directory level (I noticed you did not start-anchor those patterns, so left them alone).

You might also want to take a look at our recent thread about speeding up WordPress rewrites [webmasterworld.com]... That's some very inefficient code they've published.

Jim

jtotheh

5:16 am on Jan 23, 2010 (gmt 0)

10+ Year Member



Wow, great! That works perfectly of course, thanks so much.

I had read in a few different places that the [OR] was required because no-one could match more that IP concurrently?

I'm not sure what you mean by start-anchoring, unless that is the "^" character? I don't fully understand what that does, exactly.

I will definitely check out the WP rewrite. That must be something that would have to be re-updated if WP changed something?

Thanks again, I appreciate the help.

g1smd

6:45 pm on Jan 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You would use OR if you wanted to match A or B or C.

Here you want to match NOT A and NOT B and NOT C.

jtotheh

7:15 pm on Jan 23, 2010 (gmt 0)

10+ Year Member



Ok, I guess I don't have a good grasp on the syntax for htaccess. In my mind, i was wanting to match 'A or B or C' and exclude them, but obviously that's not correct as Jim's code worked and mine didn't.

Is below the best place to start reading to learn more?
[httpd.apache.org...]

g1smd

8:06 pm on Jan 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It is if you're using Apache 1.3 or derivatives.

There's another folder if you're using Apache 2.2.

jtotheh

8:39 pm on Jan 23, 2010 (gmt 0)

10+ Year Member



Great, thank you both for your help.