问题
I want to redirect an ip to another view of my website, for example, I want the visitor from ip x to see
www.xxx.com?_DEBUG=1
while all other visitors see the normal www.xxx.com,
how would I do this in apache config file, what directives are used?
回答1:
Here is an example of a rewrite configuration to do what you want - put this inside the virtual host for www.xxx.com on your server:
RewriteCond %{REMOTE_ADDR} 1.2.3.4
RewriteCond %{QUERY_STRING} !_DEBUG=1
#RewriteRule ^/(.*)$ /$1?_DEBUG=1 [QSA,R,L]
RewriteRule ^/(.*)$ /$1?_DEBUG=1 [QSA,L]
A couple of notes:
- Change
1.2.3.4
to whatever IP you need - The second
RewriteCond
prevents URLs already re-written to include_DEBUG=1
from being re-written again - There are two versions of the actual
RewriteRule
; the first version (commented out) actually performs a redirect. Use this if you actually want the HTTP client to submit a second request to the server including the_DEBUG=1
argument. The downside is that if you're combining GET and POST data, this method will not work. - The second version of the
RewriteRule
is what I recommend you use...it doesn't perform a redirect. Instead it just appends the_DEBUG=1
parameter to the HTTP request internally in Apache before the request is handled.
来源:https://stackoverflow.com/questions/6025116/apache-http-rewrite-redirect-based-on-ip