简体   繁体   中英

web.config redirect by ip

For website security, I would like to redirect a single IP address, or a few IP addresses, to a different domain using web.config. I'm new to this. I know how to limit access or block certain IPs, but is there an easy way to redirect? Thanks!

Couldn't reply to Victor's comment, so I'll put my edited code here rather than suggest changes.

<rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="(.*)$" ignoreCase="false" />
      <conditions>
        <add input="{REMOTE_HOST}" pattern="^123\.123\.123\.123" ignoreCase="false" negate="true" />
      </conditions>
      <action type="Redirect" redirectType="Found" url="http://www.domain-to-redirect-to/coming-soon.html" />
    </rule>
  </rules>
</rewrite>

The change here is that it's redirecting to a new domain as the asker wanted, rather than to a page on the same domain.

Note that if you want to redirect to coming-soon.html on the same domain, you will need to change your match rule, otherwise you will be put into a redirect loop.

Thus:

<rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="(.*)coming-soon.html$" ignoreCase="false" negate="true" />
      <conditions>
        <add input="{REMOTE_HOST}" pattern="^123\.123\.123\.123" ignoreCase="false" negate="true" />
      </conditions>
      <action type="Redirect" redirectType="Found" url="/coming-soon.html" />
    </rule>
  </rules>
</rewrite>

To redirect certain IP address you will need to use the URL redirect rules engine available for IIS 7

Check this link for instructions on how to redirect by IP address: https://webmasters.stackexchange.com/questions/31509/web-config-to-redirect-except-some-given-ips

<rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="(.*)$" ignoreCase="false" />
      <conditions>
        <add input="{REMOTE_HOST}" pattern="^123\.123\.123\.123" ignoreCase="false" negate="true" />
      </conditions>
      <action type="Redirect" redirectType="Found" url="/coming-soon.html" />
    </rule>
  </rules>
</rewrite>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM