我将我的 python Flask Web 应用程序托管到 Fluxflex 云托管(Apache、FastCGI)。因此,.htaccess 文件如下所示:
RewriteEngine On
AddHandler fcgid-script .fcgi
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /dispatch.fcgi/$1 [QSA,L]
它有效。现在我想从http重定向到https。我怎样才能做到这一点?添加了以下内容 .htaccess 末尾的两行:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
但是,得到响应:错误 310 (net::ERR_TOO_MANY_REDIRECTS)。如果我在第一次重写之前插入这两行,则响应为:找不到页面。
有人可以告诉我重定向到 https 的正确方法吗?
最佳答案
在 fcgi 调用之前添加一条规则以重定向到 https 此重定向必须仅在请求 http 时发生:
我的 .htaccess 看起来像这样:
Options -Indexes +FollowSymLinks +ExecCGI
<IfModule mod_fcgid.c>
AddHandler cgi-script .fcgi
RewriteEngine On
#redirect to https if http, notice the L tag
#it will break the rule processing and trigger the redirection
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://${HTTP_HOST}/$1 [R,QSA,L]
#handle the request (must be https at this point)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L]
</IfModule>
关于Apache .htaccess,url 重写问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10098370/