Apache .htaccess,url 重写问题

标签 apache .htaccess https fastcgi

我将我的 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/

相关文章:

python - Django - 在生产中提供媒体/上传文件

php - mod_rewrite {QUERY_STRING}

wordpress - 从 wordpress htaccess 重定向到 http 到 https

ssl - 使用 https 时基于主机的 nginx 代理

java - 如何使用 jetty 的 java keytool 创建/加载 jks 文件?

php - Apache 列出目录文件而不是 index.php

php - Windows 上的 LDAP 身份验证

javascript - 内部问号或 angularjs 的 url 出现问题

php - laravel 的组域路由在 laragon 中不起作用

reactjs - https 在 React Native axios 中未定义