2

I hosted my python flask web app to fluxflex cloud hosting (Apache, FastCGI). So, .htaccess file looks like the following:

RewriteEngine On
AddHandler fcgid-script .fcgi
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /dispatch.fcgi/$1 [QSA,L]

It works. Now I want to redirect from http to https. How can I do that? Added the following two lines at the end of .htaccess:

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

but, got response: Error 310 (net::ERR_TOO_MANY_REDIRECTS). If I insert this two lines before the first rewrite, response is: Page Not Found.

Can someone show me proper way of redirecting to https?

2 Answers 2

2

add a rule to redirect to https before the fcgi call This redirect must only occur if http is requested:

My .htaccess looks like that:

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>
Sign up to request clarification or add additional context in comments.

Comments

1

You want to use the %{HTTPS} variable:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.