简体   繁体   English

通过 htaccess 将域的 http 重定向到 https,而不是 IP

[英]Redirect http to https for domain not for IP by htaccess

I have API's that are IP based and I want HTTP to https for domain only not for IP based URLs.我有基于 IP 的 API,我希望 HTTP 到 https 仅适用于域,而不适用于基于 ZA12A69AZBA2B 的 URL。 These codes work for both IP and domain but I don't want to apply https on IP.这些代码适用于 IP 和域,但我不想在 IP 上应用 https。 please help me thanks.请帮助我,谢谢。

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Change the current code, to:将当前代码更改为:


Method 1方法一

RewriteCond %{HTTPS} off
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Change .^127\.0\.0\.1$ to your IP address..^127\.0\.0\.1$更改为您的 IP 地址。

For example:例如:

If your IP address is 172.16.254.1 change it to .^172\.16\.254\.1$如果您的 IP 地址为 172.16.254.1,请将其更改为.^172\.16\.254\.1$


OR或者


Method 2方法二

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Change example.com to your domain.将 example.com 更改为您的域。


Feel free to let me know if it worked or not.随时让我知道它是否有效。 Happy Coding!快乐编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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