【问题标题】:Apache .htaccess, url rewriting issueApache .htaccess,url 重写问题
【发布时间】:2012-04-23 07:28:15
【问题描述】:

我将我的 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 的正确方法吗?

【问题讨论】:

    标签: apache .htaccess https fastcgi


    【解决方案1】:

    您想使用%{HTTPS} 变量:

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

    【讨论】:

      【解决方案2】:

      在 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-08
        • 2011-03-03
        • 2011-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多