TomD
TomD

Reputation: 181

Htaccess, %{TIME} and %{REMOTE_ADDR} combined

I would like to set multiple rules based off a set of conditions as follows:

Only apply between 13th April 2016 5am > 11pm.

Only apply if source IP is within range

If both apply, re-direct 2x pages

RewriteCond %{TIME} >20160413050000 [NC]
RewriteCond %{TIME} <20160414230000 [NC]
RewriteCond %{REMOTE_ADDR} ^212\.74\.117\.10[3-9] [OR]
RewriteCond %{REMOTE_ADDR} ^212\.74\.117\.11[0-1] [OR]
RewriteCond %{REMOTE_ADDR} ^89\.197\.6\.236
RewriteRule ^confirm.html$ /confirm-logos.html [R=307,L,QSA]
RewriteRule ^blacklist.html$ /blacklist-logos.html [R=307,L,QSA]

The time rule works, the IP range works, but when I have multiple blocks of these they seem to conflict. Is the above correct for what im trying to achieve?

Upvotes: 0

Views: 75

Answers (1)

Croises
Croises

Reputation: 18671

The RewriteCond directive defines a rule condition. One or more RewriteCond can precede a RewriteRule directive. The following rule is then only used if both the current state of the URI matches its pattern, and if these conditions are met. http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond

You can use:

RewriteCond %{TIME} >20160413050000 [NC]
RewriteCond %{TIME} <20160414230000 [NC]
RewriteCond %{REMOTE_ADDR} ^212\.74\.117\.10[3-9] [OR]
RewriteCond %{REMOTE_ADDR} ^212\.74\.117\.11[0-1] [OR]
RewriteCond %{REMOTE_ADDR} ^89\.197\.6\.236
RewriteRule ^confirm.html$ /confirm-logos.html [R=307,L,QSA]

RewriteCond %{TIME} >20160413050000 [NC]
RewriteCond %{TIME} <20160414230000 [NC]
RewriteCond %{REMOTE_ADDR} ^212\.74\.117\.10[3-9] [OR]
RewriteCond %{REMOTE_ADDR} ^212\.74\.117\.11[0-1] [OR]
RewriteCond %{REMOTE_ADDR} ^89\.197\.6\.236
RewriteRule ^blacklist.html$ /blacklist-logos.html [R=307,L,QSA]

Or if you have many rules, reversing the test:

RewriteCond %{TIME} <20160413050000 [NC,OR]
RewriteCond %{TIME} >20160414230000 [NC]
RewriteCond %{REMOTE_ADDR} !^212\.74\.117\.10[3-9]
RewriteCond %{REMOTE_ADDR} !^212\.74\.117\.11[0-1]
RewriteCond %{REMOTE_ADDR} !^89\.197\.6\.236
RewriteRule ^ - [L]

RewriteRule ^confirm.html$ /confirm-logos.html [R=307,L,QSA]
RewriteRule ^blacklist.html$ /blacklist-logos.html [R=307,L,QSA]

Upvotes: 2

Related Questions