.htaccess:http到https重定向不起作用 [英] .htaccess : http to https redirection does not work

查看:105
本文介绍了.htaccess:http到https重定向不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.htaccess和https重定向出现问题.

I have a problem with my .htaccess and https redirection.

首先,我参考了这些链接以找到解决方案,但是没有一个可以帮助我.

First of all, I have consulted these links to find a solution but none could help me.

链接列表:

  • .htaccess redirect http to https
  • ModRewrite with HTTPS
  • https://www.ndchost.com/wiki/apache/redirect-http-to-https
  • htaccess redirect to https://www
  • https://serverfault.com/questions/116206/how-do-i-use-htaccess-to-always-redirect-from-http-to-https

当我使用http加载页面时,.htaccess将我重定向到https,但是当我使用https加载页面时,则出现了无限循环.

When I load the page with http, the .htaccess redirect me to https but when I load the page with https, I have an infinite loop.

这是我的.htaccess代码:

Here is my .htaccess code :

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteCond %{HTTP_HOST} ^(api|www|dev|stefano|sav)\.
RewriteCond %{REQUEST_URI} !^/(api|www|dev|stefano|sav)/
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^(.*)$ %1/$1 [L]

有人可以帮助我创建重定向条件(从http到https)吗?

Is anybody can help me create a redirect condition (http to https) ?

推荐答案

您要放弃Rewrite Flags,您需要使用R标志和可选的提供状态代码(强制建议使用301、302等)告诉它强制重定向.

You are leaving off the Rewrite Flags You need to tell it force the redirection with R flag and optional provide status code which is recommended 301, 302 etc.

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

顺便说一句,链接到的每个示例都使用R flag显示答案,不知道为什么不使用确切的示例:)

By the way, every example you linked to shows the answer using the R flag, not sure why you didn't use exact examples :)

如果在负载均衡器等设备后面,则为解决方案.

Solution if behind devices like load balancer.

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

这篇关于.htaccess:http到https重定向不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆