I've read lots of questions on this general topic on SO but can't seem to get a solution that solves my issue.
We're using ExpressionEngine and need to only have one section behind https:// We want the rest of the website to redirect any https:// request to http://.
Example #1: http://test.com/facebook 301 redirect to https://test.com/facebook
Example #2: https://test.com/test_page/ 301 redirect to http://test.com/test_page/
Note: /facebook is not a directory on the server, it is a template group inside ExpressionEngine
Here is my .htacess so far:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Force SSL on facebook pages
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^/facebook
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ https://%2/$1 [R=301,L,QSA]
# Remove SSL on other pages
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/facebook
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L,QSA]
# Remove www. from URLs
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Magic ExpressionEngine rule to remove index.php requirement from URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
The current problem: Example #2 works fine, and URLs with https:// are getting the https:// removed.
But if you go to http://test.com/facebook, it redirects to http://test.com/index.php/facebook. i.e. no HTTPS and the addition of index.php.
We think that the .htaccess file is being run twice, the first time it is hitting the first block and getting redirected, but then it gets 'index.php' added to it, and hits the second block, therefore removing the https://.
Any help would be massively appreciated.
Thanks!
Live HTTP Headers report
http://test.com/facebook/register
GET /facebook/register HTTP/1.1
HTTP/1.1 301 Moved Permanently
Date: Thu, 20 Dec 2012 17:20:17 GMT
Server: Apache
Location: https://test.com/facebook/register
--------
http://test.com/index.php/facebook/register
GET /index.php/facebook/register HTTP/1.1
HTTP/1.1 200 OK
Date: Thu, 20 Dec 2012 17:20:17 GMT
Server: Apache
So the facebook page is also created by EE. Now it makes sense. The url http://example.com/facebook
is redirect to the https version. Then it is rewritten to the EE url /index.php/facebook
, and it no longer matches ^/facebook
so it is again redirected to http://example.com/index.php/facebook
This could e.g. be fixed by using the following.
# Remove SSL on other pages
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/facebook
RewriteCond %{REQUEST_URI} !^/index\.php/facebook
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L,QSA]