You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.htaccess强制HTTPS规则中添加HTTP请求例外?

解决方法

首先要明确:你遇到的问题和.htaccess的重定向规则无关,这是浏览器的混合内容限制导致的——当你的网站运行在HTTPS环境下时,浏览器会阻止页面发起HTTP协议的请求,避免安全风险。

正确的解决步骤

直接将fetch请求的地址从HTTP改为HTTPS即可,该API支持HTTPS协议:

fetch('https://universities.hipolabs.com/search?country=canada')
  .then(response => response.json())
  .then(data => showUnis(data));

补充:如果是自己服务器的请求需要跳过HTTPS强制

如果是针对自己服务器上的某个路径需要例外(比如内部API接口),可以在.htaccess的强制HTTPS规则前添加例外条件:

RewriteEngine On

# 添加例外:匹配指定路径时不强制HTTPS
RewriteCond %{REQUEST_URI} !^/your-api-path$
# 原有的强制HTTPS规则
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# 其余原有规则保持不变
RewriteRule ^index.html$ / [L,R=301]

RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ /$1.html [L]

ErrorDocument 404 /404.html

将/your-api-path替换为实际需要例外的路径即可。

内容的提问来源于stack exchange,提问作者vin shaba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 11:25:57