2012-04-11 157 views
1

我将python flask web应用程序托管到fluxflex云托管(Apache,FastCGI)。因此,.htaccess文件如下所示:Apache .htaccess,url重写问题

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(净值:: ERR_TOO_MANY_REDIRECTS)。如果我在第一次重写之前插入这两行,则返回结果为:Page Not Found。

有人可以告诉我适当的方式重定向到https吗?

回答

1

您要使用的%{HTTPS}变量:

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

添加规则重定向到https的FCGI呼叫 此重定向之前必须只出现在所请求的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>