Search code examples
apache.htaccesshttpsfastcgi

Apache .htaccess, url rewriting issue


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?


Solution

  • You want to use the %{HTTPS} variable:

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