简体   繁体   中英

Redirect http to https exclude specific IP

I have used the following rule to exclude 200.200.0.17 to be redirected from http to https, but its requests (from 200.200.0.17) are still being redirected to https. Any help will be appreciated.

<VirtualHost _default_:80>
# If mod_rewrite is present, it takes precedence over mod_alias
# and it is necessary to rewrite the request to https.
  <IfModule mod_rewrite.c>
     RewriteEngine on
     RewriteCond %{REMOTE_ADDR} !^200.200.0.17
     RewriteRule (.*) https://%{HTTP_HOST}$1
  </IfModule>

# Otherwise use mod_alias to redirect.
  Redirect / https://%{HTTP_HOST}/
</VirtualHost>

It's because you have the alternative redirect set up, remove:

Redirect / https://%{HTTP_HOST}/

Or it will just keep redirecting that IP after the first rule ignores it.

After the first rule is executed, the second one is also executed. To prevent this from happening, use the last flag. Change the line in your code:

RewriteRule (.*) https://%{HTTP_HOST}$1 [L]

See: https://httpd.apache.org/docs/2.4/rewrite/flags.htm

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