.htaccess url rewrite with ssl redirection

Posted by Stuart McAlpine on Stack Overflow See other posts from Stack Overflow or by Stuart McAlpine
Published on 2010-03-14T18:35:12Z Indexed on 2010/03/14 18:45 UTC
Read the original article Hit count: 191

I'm having trouble combining a url query parameter rewrite (fancy-url) with a .htaccess ssl redirection.

My .htaccess file is currently:

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]

The fancy-url rewriting is working fine but the redirection to/from https isn't working at all.

If I replace the 2 lines containing

RewriteRule ^(.+).html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]

with

RewriteRule ^(.+).html$ https://%{HTTP_HOST}/index.php?page=$1 [QSA,L]

then the https redirection works fine but the fancy-url rewriting doesn't work.

Is it possible to combine these two?

© Stack Overflow or respective owner

Related posts about .htaccess

Related posts about ssl