Apache mod_write on IP addresses and Query string

Snippet

If a page has a query string argument that is well-defined such that it is unique to a particular page, you can trigger off that query string to redirect the page. You can also restrict access by IP address, or several IP addresses, or a range of IP addresses.

# if the string has id=6121 in the query and ...
  RewriteCond %{QUERY_STRING} id=6121
 
# ... doesn't have IP address 174.143.157.55 and ...
  RewriteCond %{REMOTE_ADDR} !^174\.143\.157\.55
 
# note that you need to escape the periods in the IP address
 
# ... doesn't have IP address 24.173.159.252 ...
  RewriteCond %{REMOTE_ADDR} !^24\.173\.159\.252
 
# conditions are automatically AND conditions
 
#  RewriteCond %{REMOTE_ADDR} !^24\.173\.159\.253
#  RewriteCond %{REMOTE_ADDR} !^99\.201\.42\.89
#  RewriteCond %{REMOTE_ADDR} !^24\.173\.159\.254
 
# .. then redirect all the URLs that match the original query string to the new page
  RewriteRule ^/(.*)$ http://club2009.upa.org/scores/women.html [R,L]
 
 
Notes: 
 
** 
 
You don't have to add ^(.*) in front of the query string, 
it'll match ALL URLs with the id=6121, so make sure that's 
the correct query string pattern.
 
** 
 
You can also chain conditions together by using [OR] at the 
end of the conditions (say, to restrict access to only 
specific IP addresses:
 
  ...
  RewriteCond %{REMOTE_ADDR} ^24\.173\.159\.253 [OR]
  RewriteCond %{REMOTE_ADDR} ^99\.201\.42\.89
  ...
 
** 
 
processing directives
 
  [R] means redirect
  [R=301] means permanent redirect
  [L] means the last conditionals, stop processing on this match
 
** 
 
Put the most restrictive conditional first.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.