title: .htaccess url rewrite with ssl redirection
body: <p>I'm having trouble combining a url query parameter rewrite (fancy-url) with a .htaccess ssl redirection.</p>
  
  <p>My .htaccess file is currently:</p>
  
  <pre><code>Options +FollowSymLinks
  Options -Indexes
  ServerSignature Off
  RewriteEngine on
  RewriteBase /
  
  # in https: process secure.html in https
  RewriteCond %{server_port} =443
  RewriteCond $1 ^secure$ [NC]
  RewriteRule ^(.+).html$ index.php?page=$1 [QSA,L]
  
  # in https: force all other pages to http
  RewriteCond %{server_port} =443
  RewriteCond $1 !^secure$ [NC]
  RewriteRule ^(.+).html$ http://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]
  
  # in http: force secure.html to https
  RewriteCond %{server_port} !=443
  RewriteCond $1 ^secure$ [NC]
  RewriteRule ^(.+).html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]
  
  # in http: process other pages as http
  RewriteCond %{server_port} !=443
  RewriteCond $1 !^secure$ [NC]
  RewriteRule ^(.+).html$ index.php?page=$1 [QSA,L]
  </code></pre>
  
  <p>The fancy-url rewriting is working fine but the redirection to/from https isn't working at all.</p>
  
  <p>If I replace the 2 lines containing</p>
  
  <pre><code>RewriteRule ^(.+).html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]
  </code></pre>
  
  <p>with </p>
  
  <pre><code>RewriteRule ^(.+).html$ https://%{HTTP_HOST}/index.php?page=$1 [QSA,L]
  </code></pre>
  
  <p>then the https redirection works fine but the fancy-url rewriting doesn't work.</p>
  
  <p>Is it possible to combine these two?</p>
  
  <p><strong><em>Edit:</em></strong></p>
  
  <p>The desired results are:</p>
  
  <pre><code>1. http://domain.com/secure.html is rewritten to https://domain.com/index.php?page=secure
  2. http://domain.com/foo.html is rewritten to http://domain.com/index.php?page=foo
  3. https://domain.com/secure.html is rewritten to https://domain.com/index.php?page=secure
  4. https://domain.com/foo.html is rewritten to http://domain.com/index.php?page=foo
  </code></pre>
  
  <p>(I had to put them in a code block as more than 1 link is not allowed for new users)</p>
  
  <p>So secure.html is always https while foo.html (all other pages) are always http.</p>
  
  <p><strong><em>Solution:</em></strong></p>
  
  <p>Thanks to Gumbo, the solution is:</p>
  
  <pre><code>Options +FollowSymLinks
  Options -Indexes
  ServerSignature Off
  RewriteEngine on
  RewriteBase /
  
  # in https: force all other pages to http
  RewriteCond %{server_port} =443
  RewriteCond $1 !^secure$ [NC]
  RewriteRule ^(.+)\.html$ http://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]
  
  # in http: force secure.html to https
  RewriteCond %{server_port} !=443
  RewriteCond $1 ^secure$ [NC]
  RewriteRule ^(.+)\.html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]
  
  # in https: process secure.html in https
  RewriteCond %{server_port} =443
  RewriteCond $1 ^secure$ [NC]
  RewriteRule ^(.+)\.html$ index.php?page=$1 [QSA,L]
  
  # in http: process other pages as http
  RewriteCond %{server_port} !=443
  RewriteCond $1 !^secure$ [NC]
  RewriteRule ^(.+)\.html$ index.php?page=$1 [QSA,L]
  </code></pre>