我希望某些页面可以在 https 上使用,但在 http 上不可用。我有一个 Code Igniter 框架。
以下是 nginx conf 的代码:
server {
listen 80;
......
#enforce https
if ($request_uri ~ "^(winkelmandje|(index.php/)?winkelmandje|(index.php/)?history|(index.php/)?mobile/winkelmandje|(index.php/)?winkelmandje/voegtoe)"){
rewrite ^/(.*)$ https://$http_host/$1 permanent;
}
....
}
server {
listen 443;
ssl on;
....
#enforce http
if ($request_uri ~ "^(!(index.php/)?winkelmandje|!(index.php/)!mobile/winkelmandje|!(index.php/)?history|assets/|!lib_desktop/|!lib_mobile/|!images/|!widget/|!(index.php/)?winkelmandje/voegtoe)"){
rewrite ^/(.*)$ http://$http_host/$1 permanent;
}
.....
}
如果我通过 http 访问我的域/winkelmandje(此页面是购物车),我不会被重定向到 https。
另一个例子是,如果我通过 https 访问域名索引,则不会在 http 上重定向
答案1
删除!
https 服务器指令中的 ,并^
从 http 和 https 中删除 (因为 request_uri 也包含域)。
server {
listen 80;
......
#enforce https
if ($request_uri ~ "(winkelmandje|(index.php/)?winkelmandje|(index.php/)?history|(index.php/)?mobile/winkelmandje|(index.php/)?winkelmandje/voegtoe)"){
rewrite ^/(.*)$ https://$http_host/$1 permanent;
}
....
}
server {
listen 443;
ssl on;
....
#enforce http
if ($request_uri ~ "(!(index.php/)?winkelmandje|!(index.php/)!mobile/winkelmandje|!(index.php/)?history|assets/|!lib_desktop/|!lib_mobile/|!images/|!widget/|!(index.php/)?winkelmandje/voegtoe)"){
rewrite ^/(.*)$ http://$http_host/$1 permanent;
}
.....
}