apache http rewrite/redirect based on ip

主宰稳场 提交于 2019-12-06 08:19:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!