title: Cloudflare flexible SSL and redirects
body: <p>I'm having some really annoying problems with my domain.</p>
  
  <p>I have Cloudflare flexible SSL on my site, and it makes a https call, IF I type it myself >&lt;
  When I try to force https to be used on my site via a <code>.htaccess</code> file I get an internel server error. </p>
  
  <p>I would like all visitors to my site to always be redirected to <code>https://www.example.com</code> even if they type <code>example.com</code> or <code>example.com/index.php</code> etc. + using https on the whole site.</p>
  
  <p>I just can't find the solution to this, so I'm trying stack. </p>
  
  <p>My DNS settings on Cloudflare is an A record that points <code>domain.com</code> to a IP
  and a CNAME record that says <code>www.domain.com</code> is an alias of <code>domain.com</code></p>
  
  <p>I have no page rules added on Cloudflare atm. </p>
  
  <pre><code>    &lt;IfModule mod_setenvif.c&gt;
      &lt;IfModule mod_headers.c&gt;
          &lt;FilesMatch "\.(cur|gif|ico|jpe?g|png|svgz?|webp)$"&gt;
              SetEnvIf Origin ":" IS_CORS
              Header set Access-Control-Allow-Origin "*" env=IS_CORS
          &lt;/FilesMatch&gt;
      &lt;/IfModule&gt;
  &lt;/IfModule&gt;
  
  &lt;IfModule mod_headers.c&gt;
      &lt;FilesMatch "\.(eot|otf|tt[cf]|woff)$"&gt;
          Header set Access-Control-Allow-Origin "*"
      &lt;/FilesMatch&gt;
  &lt;/IfModule&gt;
  
  
  Options -MultiViews
  
  
  &lt;IfModule mod_headers.c&gt;
      Header set X-UA-Compatible "IE=edge"
      # `mod_headers` cannot match based on the content-type, however, this
      # header should be send only for HTML pages and not for the other resources
      &lt;FilesMatch "\.(appcache|atom|crx|css|cur|eot|f4[abpv]|flv|gif|htc|ico|jpe?g|js|json(ld)?|m4[av]|manifest|map|mp4|oex|og[agv]|opus|otf|pdf|png|rdf|rss|safariextz|svgz?|swf|tt[cf]|vcf|vtt|webapp|web[mp]|woff|xml|xpi)$"&gt;
          Header unset X-UA-Compatible
      &lt;/FilesMatch&gt;
  &lt;/IfModule&gt;
  
  &lt;IfModule mod_mime.c&gt;
  
    # Audio
      AddType audio/mp4                                   m4a f4a f4b
      AddType audio/ogg                                   oga ogg opus
  
    # Data interchange
      AddType application/json                            json map
      AddType application/ld+json                         jsonld
  
      AddType application/javascript                      js
  
    # Video
      AddType video/mp4                                   f4v f4p m4v mp4
      AddType video/ogg                                   ogv
      AddType video/webm                                  webm
      AddType video/x-flv                                 flv
  
    # Web fonts
      AddType application/font-woff                       woff
      AddType application/vnd.ms-fontobject               eot
  
  
      AddType application/x-font-ttf                      ttc ttf
      AddType font/opentype                               otf
  
  
      AddType     image/svg+xml                           svgz
      AddEncoding gzip                                    svgz
  
    # Other
      AddType application/octet-stream                    safariextz
      AddType application/x-chrome-extension              crx
      AddType application/x-opera-extension               oex
      AddType application/x-web-app-manifest+json         webapp
      AddType application/x-xpinstall                     xpi
      AddType application/xml                             atom rdf rss xml
      AddType image/webp                                  webp
      AddType image/x-icon                                cur
      AddType text/cache-manifest                         appcache manifest
      AddType text/vtt                                    vtt
      AddType text/x-component                            htc
      AddType text/x-vcard                                vcf
  
  &lt;/IfModule&gt;
  
  AddDefaultCharset utf-8
  
  
  &lt;IfModule mod_mime.c&gt;
      AddCharset utf-8 .atom .css .js .json .jsonld .rss .vtt .webapp .xml
  &lt;/IfModule&gt;
  
  
  #&lt;IfModule mod_rewrite.c&gt;
  #     Options +FollowSymlinks
     # Options +SymLinksIfOwnerMatch
  #    RewriteEngine On
     # REDIRECT /folder/index.php to /folder/
  #    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
  #    RewriteRule ^(([^/]+/)*)index\.php$ http://www.domain.dk/$1 [R=301,L]
  #&lt;/IfModule&gt;
  
  
  #&lt;IfModule mod_rewrite.c&gt;
  #    RewriteCond %{HTTPS} !=on
  #    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  #    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
  #&lt;/IfModule&gt;
  
  #&lt;IfModule mod_rewrite.c&gt;
  #    RewriteCond %{HTTPS} !=on
  #    RewriteCond %{HTTP_HOST} !^www\. [NC]
  #    RewriteCond %{SERVER_ADDR} !=127.0.0.1
  #    RewriteCond %{SERVER_ADDR} !=::1
  #    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  #&lt;/IfModule&gt;
  
  
  &lt;IfModule mod_autoindex.c&gt;
      Options -Indexes
  &lt;/IfModule&gt;
  
  &lt;IfModule mod_rewrite.c&gt;
      RewriteCond %{SCRIPT_FILENAME} -d [OR]
      RewriteCond %{SCRIPT_FILENAME} -f
      RewriteRule "(^|/)\." - [F]
  &lt;/IfModule&gt;
  
  &lt;FilesMatch "(^#.*#|\.(bak|config|dist|fla|in[ci]|log|psd|sh|sql|sw[op])|~)$"&gt;
  
      # Apache &lt; 2.3
      &lt;IfModule !mod_authz_core.c&gt;
          Order allow,deny
          Deny from all
          Satisfy All
      &lt;/IfModule&gt;
  
      # Apache ≥ 2.3
      &lt;IfModule mod_authz_core.c&gt;
          Require all denied
      &lt;/IfModule&gt;
  
  &lt;/FilesMatch&gt;
  
  &lt;IfModule mod_headers.c&gt;
       Header set X-Content-Type-Options "nosniff"
  &lt;/IfModule&gt;
  
  #&lt;IfModule mod_rewrite.c&gt;
  #    RewriteCond %{SERVER_PORT} !^443
  #    RewriteRule ^ https://www.domain.dk%{REQUEST_URI} [R=301,L]
  #&lt;/IfModule&gt;
  
  
  
  &lt;IfModule mod_deflate.c&gt;
  
      # Force compression for mangled headers.
      &lt;IfModule mod_setenvif.c&gt;
          &lt;IfModule mod_headers.c&gt;
              SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
              RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
          &lt;/IfModule&gt;
      &lt;/IfModule&gt;
  
      # Compress all output labeled with one of the following MIME-types
      # (for Apache versions below 2.3.7, you don't need to enable `mod_filter`
      #  and can remove the `&lt;IfModule mod_filter.c&gt;` and `&lt;/IfModule&gt;` lines
      #  as `AddOutputFilterByType` is still in the core directives).
      &lt;IfModule mod_filter.c&gt;
          AddOutputFilterByType DEFLATE application/atom+xml \
                                        application/javascript \
                                        application/json \
                                        application/ld+json \
                                        application/rss+xml \
                                        application/vnd.ms-fontobject \
                                        application/x-font-ttf \
                                        application/x-web-app-manifest+json \
                                        application/xhtml+xml \
                                        application/xml \
                                        font/opentype \
                                        image/svg+xml \
                                        image/x-icon \
                                        text/css \
                                        text/html \
                                        text/plain \
                                        text/x-component \
                                        text/xml
      &lt;/IfModule&gt;
  
  &lt;/IfModule&gt;
  
  
  &lt;IfModule mod_headers.c&gt;
     Header set Cache-Control "no-transform"
  &lt;/IfModule&gt;
  
  &lt;IfModule mod_headers.c&gt;
      Header unset ETag
  &lt;/IfModule&gt;
  
  FileETag None
  
  #&lt;IfModule mod_expires.c&gt;
  
  #ExpiresActive On
  #ExpiresByType image/jpg "access 1 year"
  #ExpiresByType image/jpeg "access 1 year"
  #ExpiresByType image/gif "access 1 year"
  #ExpiresByType image/png "access 1 year"
  #ExpiresByType text/css "access 1 month"
  #ExpiresByType text/html "access 1 month"
  #ExpiresByType application/pdf "access 1 month"
  #ExpiresByType text/x-javascript "access 1 month"
  #ExpiresByType application/x-shockwave-flash "access 1 month"
  #ExpiresByType image/x-icon "access 1 year"
  #ExpiresDefault "access 1 month"
  
  #&lt;/IfModule&gt;
  </code></pre>