【问题标题】:Forcing HTTPS in htaccess while rewriting a link重写链接时在 htaccess 中强制使用 HTTPS
【发布时间】:2026-01-07 17:55:02
【问题描述】:

我正在尝试在 htaccess 中重写链接时强制使用 HTTPS,但运气不佳...

这是我的原始代码..

RewriteRule ^Shopping-Cart/ shoppingCart/shoppingCart.php [L]

然后我添加了代码来强制使用 HTTPS 所以..

RewriteCond %{HTTPS} !=on
RewriteRule ^Shopping-Cart/ https://myserver.com/shoppingCart/shoppingCart.php [L]

但是当从链接加载myserver.com/Shopping-Cart/ 时,我总是以https://myserver.com/shoppingCart/shoppingCart.php 结束

有人可以给我提示吗?

谢谢!

【问题讨论】:

  • 似乎你不能在这里使用'!='。请改用“=off”。

标签: php apache .htaccess mod-rewrite https


【解决方案1】:

发生这种情况是因为我相信您无法隐藏它是安全链接的事实。即https。因此,即使 Apache 设置为使用 [L],它仍然会进行重定向,因为它正在更改为 https 协议。

您需要先更改 htaccess 以加载 https,然后再处理 mod_rewrites。 试试吧,

RewriteEngine On

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

RewriteRule ^Shopping-Cart/? shoppingCart/shoppingCart.php [L]

【讨论】: